Docker Setup Mysql

2/13/2022by admin

I want to set up a MySQL environment using Docker desktop for Windows.

For creating MySQL as a Docker container, the host machine should have Docker installed. If you do not have it installed, here’s a step-by-step guide how to install Docker on Ubuntu. Pull the MySQL Docker Image. The first thing you have to do to create and test the MySQL server in Docker is to pull a MySQL official image from the Docker Hub. Docker Compose installed on your server, following Step 1 of How To Install Docker Compose on Ubuntu 18.04. A registered domain name. This tutorial will use throughout. You can get one for free at Freenom, or use the domain registrar of your choice. Both of the following DNS records set up for your server.

I want to mount my.cnf by volumes in docker-compose.yml, but my.cnf is not copied to docker.

I checked various websites and found that on Windows, I need to grant permissions to the copied my.cnf, but that doesn’t work either.
(In the first place, my.cnf is not created in docker.

Is there any way to solve this problem?
Any advice would be appreciated in advance.

Just in case, here is my docker configuration file.

  • docker-compose.yml
  • ./mysql/my.cnf
  • dockerfile

This guide explains step-by-step how to set up a new MySQL server running in Docker container in only a few minutes.

One of the great things about Docker is how you can quickly use it to try out applications without having to install it directly on your machine. You can use Docker to run a database in a container as if it were a remote server, and test how your application interacts with it.

Running MySQL with Docker containers is a widely used mechanism. In fact MySQL is one of the most popular database used with Docker containers.

For creating MySQL as a Docker container, the host machine should have Docker installed. If you do not have it installed, here’s a step-by-step guide how to install Docker on Ubuntu.

1. Pull the MySQL Docker Image

The first thing you have to do to create and test the MySQL server in Docker is to pull a MySQL official image from the Docker Hub.

You can download a specific version such as 8.0, 5.7, 5.6, or opt for the latest release as seen in the following command:

If you want a particular version of MySQL, replace latestwith the version number.

You can run the docker images command to see a list of Docker images you have pulled from the Docker Hub.

As you can see, the MySQL image is now available to our locally installed Docker.

2. Setting Up a Docker Volume

As you know, Docker containers are in principle ephemeral, and any data or configuration are expected to be lost if the container is deleted. Docker volumes, however, provides a mechanism to persist data created inside a Docker container. They are preferred mechanism by the Docker for storing persistent data of Docker containers.

By default, MySQL stores its data files in the /var/lib/mysql directory. So before deploying, you’ll need to setup a Docker volume to persist your database in. Otherwise, your data will be lost when the container restarts.

Let’s create a new volume and named it mysql-data. This will be used to store all your database information.

You can list all the volumes known to Docker by issuing the command below:

As you can see, our newly created Docker volume named mysql-data is here and ready to serve data. It can then be attached to a newly spun MySQL container.

3. Run a MySQL Docker Container

Now that you have MySQL Docker image on your machine and a volume to persist the data, you can deploy a container. You must also set a password for the MySQL root user by using the MYSQL_ROOT_PASSWORD environment variable.

Now, let’s run the container:

That’s it. This will start the latest version of MySQL instance, which can be remotely accessible from anywhere with specified root password.

To make things clearer, let’s break down this syntax. Here is what each parameter in that command means:

Docker Setup Mysql Server

  • -d will run this container in detached mode so that it runs in the background.
  • --name assigns the name mysql-server to your container instance. If you don’t specify this, Docker will generate a random name.
  • -p will bind the MySQL container port 3306 to the same port on your host machine. You’ll be able to connect to on port 3306 using MySQL clients (mysql) running on your host.
  • -v option bind that data folder inside the container volume (/var/lib/mysql) to the local Docker volume (mysql-data) you created in the previous step.
  • -e sets an environment variable. In this case, the MySQL root password.
  • mysql is the name of the image we use to create the container.

You can check whether the container is running by listing the running containers:

Related: MySQL: How to Backup (Dump) and Restore Database Using mysqldump

4. Connecting to the MySQL Container


You can gain an interactive MySQL shell by attaching to the container and running the mysql command. This provides the full MySQL command-line interface, so you can use all the familiar commands and flags.

Related: How to Create a Database in MySQL Using the Command Line

You can also connect with the MySQL Docker instance by using the mysql client if you have it pre-installed on your host.

Beware that by default, the mysql client tries to connect using a unix socket when you tell it to connect to localhost. So do use and not localhost.

Of course, you can also use a GUI-based client such as MySQL Workbench to connect to the database. In your MySQL Workbench for “Hostname” specify the IP address of the machine on which you started the Docker Container.

Related: How to Create a Database in MySQL with MySQL Workbench

Docker Setup Mysql

Of course in the examples shown above for a hostname you can use the IP address of the machine on which the MySQL docker container is working instead of You can find this address by running the ifconfig command without any arguments to it.

Using the machine’s IP address allows you to remotely access the MySQL server.

5. Stop, Start, and Restart MySQL Container

Docker mysql setup script

This command will temporarily stop the running MySQL container (and can be resumed/restarted later):

To start the MySQL container run:

Docker How To Setup Mysql


The command below is used to restart the running MySQL container:


Docker Install Mysql 8

As you can see, running MySQL within Docker is much quicker and easier than installing MySQL Server locally. There are a lot more things to consider if you’re going to use MySQL on Docker for anything beyond testing. For more information you can check-out the MySQL page on Docker Hub and the official MySQL manual on the subject.

Let me know if you have any questions or suggestions. I’ll be happy to follow-up with you.

Comments are closed.