Start mysql docker container with docker-compose

Start mysql docker container with docker-compose
Page content

In the previous article, I can start mysql docker container by using the docker run command after executing docker build.

Personally, I like the lesser option of cli, so this time I’d like to start docker-compose a bit easier to start up.

Install docker-compose

Let’s install docker-toolbox from the following site. Docker-compose is also included in the download.

Docker-compose provides functions to easily manage multiple containers. In this article, only mysql is started with a container, but since setting up system components and controlling the order of activation can only be described in yaml file, it is highly readable and easy to manage even with VCS.

For example, suppose you have the following Dockerfile .

 1FROM mysql:latest
 2
 3RUN { \
 4   echo '[mysqld]'; \
 5   echo 'character-set-server=utf8'; \
 6   echo 'collation-server=utf8_general_ci'; \
 7   echo '[client]'; \
 8   echo 'default-character-set=utf8'; \
 9} > /etc/mysql/conf.d/charset.cnf
10
11EXPOSE 3306
12CMD ["mysqld"]

Create a docker-compose.yml that uses Dockerfile .

 1mysql:
 2  build: .
 3  dockerfile: Dockerfile
 4  ports:
 5    - "3306:3306"
 6  environment:
 7    - MYSQL_ROOT_USER=root
 8    - MYSQL_ROOT_PASSWORD=root
 9    - MYSQL_DATABASE=soudegesu
10    - MYSQL_USER=soudegesu
11    - MYSQL_PASSWORD=soudegesu
12  volumes:
13    - ./init.d:/docker-entrypoint-initdb.d

By specifying the created Dockerfile as the dockerfile property of the yaml file, you can build the image in the startup phase and launch the container with the created image.

In the case of an official container of mysql, by attaching docker-entrypoint-initdb.d inside the container to an arbitrary directory on the host, sql in the directory is executed when the container is started, and the database is initialized.

launch with docker-compose

Let’s start it.

 1docker-compose up
 2
 3Starting soudegesu_mysql_1
 4Attaching to soudegesu_mysql_1
 5mysql_1  | 2017-01-31T07:09:28.026908Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
 6mysql_1  | 2017-01-31T07:09:28.038354Z 0 [Note] mysqld (mysqld 5.7.17) starting as process 1 ...
 7mysql_1  | 2017-01-31T07:09:28.053912Z 0 [Note] InnoDB: PUNCH HOLE support available
 8mysql_1  | 2017-01-31T07:09:28.054031Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
 9mysql_1  | 2017-01-31T07:09:28.054043Z 0 [Note] InnoDB: Uses event mutexes
10mysql_1  | 2017-01-31T07:09:28.054055Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
11...

Try connecting to the database with the mysql command.

 1mysql -h 127.0.0.1 -P 3306 -u root -proot
 2mysql: [Warning] Using a password on the command line interface can be insecure.
 3Welcome to the MySQL monitor.  Commands end with ; or \g.
 4Your MySQL connection id is 3
 5Server version: 5.7.17 MySQL Community Server (GPL)
 6
 7Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
 8
 9Oracle is a registered trademark of Oracle Corporation and/or its
10affiliates. Other names may be trademarks of their respective
11owners.
12
13Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14
15mysql>

I did it. Also check the execution result of sql at database startup.

 1mysql> show databases;
 2+--------------------+
 3| Database           |
 4+--------------------+
 5| information_schema |
 6| soudegesu              |
 7| mysql              |
 8| performance_schema |
 9| sys                |
10+--------------------+
 1mysql> use soudegesu;
 2Reading table information for completion of table and column names
 3You can turn off this feature to get a quicker startup with -A
 4
 5Database changed
 6mysql> show tables;
 7+-----------------+
 8| Tables_in_soudegesu |
 9+-----------------+
10| aaaa            |
11| bbbb            |
12+-----------------+
132 rows in set (0.00 sec)

Additional Information

You can also run the docker-compose up command using the existing container image. If necessary, modify docler-compose.yml as follows.

 1mysql:
 2  image: mysql:latest
 3  ports:
 4    - "3306:3306"
 5  environment:
 6    - MYSQL_ROOT_USER=root
 7    - MYSQL_ROOT_PASSWORD=root
 8    - MYSQL_DATABASE=soudegesu
 9    - MYSQL_USER=soudegesu
10    - MYSQL_PASSWORD=soudegesu
11  volumes:
12    - ./init.d:/docker-entrypoint-initdb.d