How to Run MySQL in Docker Container

How to Run MySQL in Docker Container: A Simple, Easy to Follow Guide

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 installing it directly on your machine. For example, you can use Docker to run a database in a container as a remote server and test how your application interacts with it.

Running MySQL with Docker containers is a widely used mechanism. MySQL is one of the most popular databases 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 on installing Docker in 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.

MySQL on 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:

sudo docker pull mysql:latest

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

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

sudo docker images
mysql        latest    2fe463762680   13 hours ago   514MB

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, temporary, and any data or configuration are expected to be lost if the container is deleted.

Docker volumes, however, provide a mechanism to persist data created inside a Docker container. Therefore, they are Docker’s preferred mechanism for storing persistent data of Docker containers.

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

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

sudo docker volume create mysql-data

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

sudo docker volume ls
local     mysql-data

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 freshly spun MySQL container.

3. Run a MySQL Docker Container

Now that you have a 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:

sudo docker run -d --name=mysql-server -p 3306:3306 -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=your_password mysql

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

To make things more transparent, let’s break down this syntax. First, here is what each parameter in that command means:

  • -d will run this container in a 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:

sudo docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
0fc229e3df77   mysql     "docker-entrypoint.s…"   37 minutes ago   Up 34 minutes>3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql-server

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 it to the container and running the mysql command. This provides the full MySQL command-line interface to use all the familiar commands and flags.

sudo docker exec -it mysql-server mysql -u root -p
Attaching to the MySQL Docker Container

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

You can also connect with the MySQL Docker instance 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.

mysql -u root -h -p

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

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

Of course, in the examples shown above for a hostname, you can use the machine’s IP address on which the MySQL docker container is working instead of

You can find this address by running the ifconfig command without any arguments.

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

5. Stop, Start, and Restart MySQL Container

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

sudo docker stop mysql-server

To start the MySQL container, run:

sudo docker start mysql-server

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

sudo docker restart mysql-server


As you can see, running MySQL within Docker is much quicker and easier than installing MySQL Server locally. However, there are many more things to consider if you’re going to use MySQL on Docker for anything beyond testing.

You can check out the MySQL page on Docker Hub and the official MySQL manual on the subject for more information.

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

Leave a Reply