Create database when running docker-compose
I would like to create a MySQL database using environment variables in the docker-compose.yml file, but it doesn't work. I have the following code:
# The Database
database:
image: mysql:5.7
volumes:
- dbdata:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: homestead
MYSQL_USER: root
MYSQL_PASSWORD: secret
ports:
- "33061:3306"
Can anyone explain the function of these vars?
source to share
It is also possible to provide an initialization file for the container mysql
that will be used each time the container is created.
database:
image: mysql:5.7
ports:
- "33061:3306"
command: --init-file /data/application/init.sql
volumes:
- ./init.sql:/data/application/init.sql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: homestead
MYSQL_USER: root
MYSQL_PASSWORD: secret
Such a file could contain your original database structure and data - for example:
CREATE DATABASE IF NOT EXISTS dev;
CREATE DATABASE IF NOT EXISTS test;
USE dev;
CREATE TABLE IF NOT EXISTS (...);
source to share
The database is probably already initialized and the configuration is stored in /var/lib/mysql
. Since you have defined a volume for this location, the configuration will go into reboot mode. The MySQL image will not reconfigure the database over and over, it only does it once.
volumes:
- dbdata:/var/lib/mysql
If your database is empty, you can reset the database by doing docker-compose down -v
where -v
deletes the volumes defined in the volume partition. See https://docs.docker.com/compose/reference/down/ . On the next docker-compose up
, the MySQL image will start a new one and initialize the database using the configuration you provided in the environment section.
source to share
For version 2 of the docker command, you can .yml
either .yaml
look like this:
version: '2'
volumes:
dbdata:
services:
mysql:
image: mysql:5.7
container_name: mysql
volumes:
- dbdata:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=homestead
- MYSQL_USER=root
- MYSQL_PASSWORD=secret
ports:
- "33061:3306"
run it with docker-compose up -d
and check:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3567fb78d0d mysql:5.7 "docker-entrypoint..." 2 minutes ago Up 2 minutes 0.0.0.0:33061->3306/tcp mysql
docker exec -it a3567fb78d0d bash
root@a3567fb78d0d:/# mysql -u root -p homestead
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| homestead |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
The volume will persist in docker volume nameoffolder_dbdata
( /var/lib/docker/volumes/...
)
source to share
If I understand your question correctly, you want to have a container with a specific database in it. For example, you already have a MySQL container with CREATE DATABASE mydb
, etc. If so, you need to use docker-entrypoint-initdb.d
: https://docs.docker.com/samples/library/mysql/#docker-secrets
When the official MySQL container is launched for the first time, a new database is created first. Then it will execute the .sh, .sql and .sql.gz files that are in /docker-entrypoint-initdb.d
. So all you have to do is create a directory /docker-entrypoint-initdb.d
and put your init script there.
source to share
Answering your question ...
One thing I use when creating a new docker container is to understand which image I am pulling when it is built.
Your docker-compose.yml tou has this
# The Database
database:
image: mysql:5.7
This is the image you are pulling from "mysql: 5.7"
Dockerhub is the repository where you can find information about these images.
Do a google search for "mysql: 5.7 dockerhub"
First result https://hub.docker.com/_/mysql/
There you have image 5.7, if you click 5.7 you have this
https://github.com/docker-library/mysql/blob/607b2a65aa76adf495730b9f7e6f28f146a9f95f/5.7/Dockerfile
What is the Dockerfile from the image, you can take a look at the interesting things that come up when creating the image.
One of them is ENTRYPOINT ["docker -entrypoint.sh"]
This is the file that was executed when the image is ready
I will go one level in the repo, you will see this file
https://github.com/docker-library/mysql/tree/607b2a65aa76adf495730b9f7e6f28f146a9f95f/5.7
You can see which environment variables are used to create a new database, etc.
file_env 'MYSQL_DATABASE'
if [ "$MYSQL_DATABASE" ]; then
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
mysql+=( "$MYSQL_DATABASE" )
fi
source to share