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

Since the Docker has become a popular DevOps tools, you'll probably need to know how to run MySQL inside a Docker container.

How to Run MySQL in Docker Container

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.

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 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
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
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 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.

sudo docker volume create mysql-data

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

sudo docker volume ls
DRIVER    VOLUME NAME
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 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:

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

  • -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 127.0.0.1 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   0.0.0.0:3306->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 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.

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 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 127.0.0.1 and not localhost.

mysql -u root -h 127.0.0.1 -p

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

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 127.0.0.1. 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

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

Conclusion

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.

If this guide has helped you, please consider buying us a coffee.

Buy me a coffee!

Your support and encouragement are greatly appreciated!

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge