From:       To:      
Home > Documentation > MySQL

Running MySQL in Docker

Once MySQL is containerized in Docker, it becomes isolated from other apps running in the same system. It makes maintenance and administration routines much easier.

Besides, building an application in a Docker Container guaranties repeatable configuration and infrastructure, that significantly increases speed of the development and deployment.

So, to run MySQL in a Docker container you should take these generic steps:

Get the MySQL Docker Image

You can pull the most recent version of Docker image for MySQL by running the following command:

sudo docker pull mysql/mysql-server:latest
After the pulling procedure is completed, you should verify that the appropriate image has been stored locally as follows:

sudo docker images

Docker image for MySQL is stored properly if the listed images contain "mysql/mysql-server".

Deploy MySQL Container

Once you pulled the Docker image from repository, it is time to deploy new MySQL container through the following command line statement:

sudo docker run --name={container name} -d mysql/mysql-server:latest

Replace {container name} with the desired container name. If you do not specify the container name, Docker generate it by default. After the container is deployed, you should verify that the Docker MySQL container is running as following:

docker ps

Connect to the Docker Container

Before connecting MySQL Container with the host, you have to verify that MySQL Client Package has been installed:

apt-get install mysql-client

Next step is to find the generated root password in the log file of the MySQL container:

sudo docker logs {container name}

In the output find the following pattern: [Entrypoint] GENERATED ROOT PASSWORD:. Copy and paste the password following next that pattern into a text editor for future use. Navigate mouse pointer to bash shell of the MySQL Container and type out the following command:

sudo docker exec -it {container_name} bash

Enter the saved root password when it is prompted. Once connection is established, you can change the MySQL root password:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password';

Configuring Docker MySQL Container

All the configuration parameters are stored in the file "/etc/mysql/my.cnf". In order to change the configuration, you have to create an alternative config file on the host machine and mount it to the MySQL Container through the following steps:

Combine configuration from new file with default config parameters by mapping the volume path in the command line that runs the MySQL Docker Container:

docker run --detach --name={container name} ... 
--volume=/root/docker/{container name}/conf.d:/etc/mysql/conf.d

Connecting Docker MySQL with Host Machine

First, you need to map the Docker Container port to a Docker MySQL port on localhost that will be used to connect to MySQL. Stop and remove the previously created container:

docker stop {container name}
docker rm {container name}

Once the old container is removed, it is time to start new container with the same MySQL image and map port of the Docker Container to the localhost port:

docker run -p 13306:3306 --name {new container name} -eMYSQL_ROOT_PASSWORD={password} -d mysql:latest

Make sure that the command has been executed without errors. In this case it must print the generated container ID. Now you can connect to MySQL from localhost by MySQL Workbench or the command line:

mysql --host= --port=13306 -u root -p

Managing MySQL Container

You can start the Docker MySQL Container using the following command line:

sudo docker start {container name}

You can stop the Docker Container for MySQL using the following command line:

sudo docker stop {container name}

To restart the MySQL Container use the following command line:

sudo docker restart {container name}

To delete the Docker MySQL Container, stop it and then remove it as follows:

sudo docker rm {container name}

To create MySQL database dump, use "docker exec" statement and "mysqldump" command line tool designed for this purpose:

docker exec -i {container name} sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /path/on/the/host/database.sql

To restore MySQL database from the dump file, use the following command line:

docker exec -i {container name} sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /path/on/the/host/database.sql

Have questions? Contact us