How to Set Up MySQL in a Docker Container


If you want to set up a MySQL database to store data for websites, blogs, or applications, you can use a Docker container. It’s relatively simple to do. And like shuffling a deck of cards, reversing a string in Python, or writing a recursive function, it’s a suitable exercise for beginners just starting out with MySQL and backend web development.

Let’s take a look at exactly what a Docker container is, and how you can use it to set up MySQL.

Image by VLADGRIN on Shutterstock

What Is a Docker Container?

A Docker container is a self-contained software development environment that includes all of the necessary components to run an application. This can include the application code, libraries, and other dependencies.

Containers are isolated from one another and from the host operating system, so they can’t interfere with or access other containers or the host OS. This makes them ideal for packaging and deploying applications.

Docker containers are created using a Docker image. This is simply a template for creating a container. You can create your own Docker images or use images that others have created. When you create a container from a Docker image, you can customize it by adding your own files, settings, and applications.

In this tutorial, we will be looking at how to set up MySQL in a Docker container.

Please note that commands might be different based on the OS installed. This example was made using an Arch Linux-based operating system and MySQL 8.0.

Step 1. Install Docker

If you haven’t already, install Docker. First, ensure that the OS install is up to date with the following command in a new terminal window.

$ sudo pacman -Syu

Once it has been verified that the system is up to date, install Docker.

$ sudo pacman -S docker

Step 2. Install MySQL in Docker

Start the Docker service and run the command “Docker images” to check if MySQL is currently installed.

[root@docker ~]# docker images

The Newsletter for PHP and MySQL Developers

Step 3. Start a MySQL Server Instance

MySQL can be run as an instance. Use -d to run the container in detached mode and name the Docker container. MySQL uses the port 3600:3306 by default. Be sure to set the password for the database. For this example, it’s simply set to “password.”

[root@docker ~]# docker container run -d –name mysql1 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password mysql:latest

As mysql:latest is not available locally, this command will begin to download the latest version of MySQL available.

Check with the command “Docker ps” that the MySQL container is running.

[root@docker ~]# docker ps

This command should show the container ID, and the ports the MySQL container is running on.

Sample output:

6bcd1d829631 mysql:latest “docker-entrypoint…” 5 seconds ago Up 3 seconds 0.0.0:3306->3306/tcp, 33060/tcp

Step 4. SSH into MySQL Container

Now, we’ll need to SSH into the MySQL container with the “Docker exec” command, which is a command that runs new commands in a running container. We have verified in Step 2 that the container is running. After this, we will need to log into MySQL to create a new database.

[root@docker ~]# docker exec -it mysqldb bash

Log into the MySQL container using the password created when the container was made in Step 2, here our password is “password.” Also, ensure there is no space after -p since everything after -p is part of the inputted password.

[root@6bcd1d829631]# mysql -u root -ppassword

When issuing this command you’ll commonly encounter a warning that using a password on the command line is insecure. To get around this warning message, use the mysql_config_editor tools.

Step 5. Make a Database

Now that we are in MySQL, use the command “show databases” to list all of the available databases.

mysql> show databases;

Now create a database with the “create database” command and a name for the database. For this example, we’ll use the name “newdatabasename.”

mysql> create database newdatabasename

If we use the command “show databases” the new database should now appear in the list. See the below sample output.

| Database |
| information_schema |
| mysql |
| newdatabasename |
| performance_schema |
| sys |

This database can easily be deleted with the command “drop database” followed by the name of the database.

mysql> drop database newdatabasename;

Step 6. Install MySQL Workbench

MySQL Workbench is a graphical tool for administration purposes, and it’s available for most operating systems. Be sure to install the version that matches your OS. It can be downloaded from the MySQL website. Note that not all versions of Workbench and MySQL are compatible, so using the most recent versions will help remove compatibility issues.


Docker lets you run isolated containers to improve application performance while MySQL is extremely scalable and commonly used for database management.

Together, they’re a great combo for web developers starting to work with backend web development. If you want to get started with MySQL and Docker, follow the six steps outlined in the post and visit Docker Hub for the latest MySQL images. 

Also, subscribe to Digital Owl’s Prose to receive notifications of new blog posts by email when they are published. Feel free to submit suggestions for future articles!

Author Bio
Jody Dascalu

Jody is a freelance writer in the technology niche. She studied in Canada and earned a Bachelor of Engineering. Jody has over five years of progressive supply chain work experience and is currently employed as a business analyst. As an avid reader, she loves to research upcoming technologies and is an expert on a variety of topics.

How can I help you?

Are you thinking of starting up a blog? I use WordPress for my blog. Let’s both save money on the plans offered. Grab a Gmail HTML Email Signature template from my Etsy shop and make your emails pop and stand out. Need hosting for your next web application or WordPress site? I use and highly recommend Hostinger. They have great pricing and service.I enjoy reading Refind: The essence of the web, every morning in your inbox. Subscribe for free. Help me get a premium subscription by signing up yourself with my referral link.Grab a free pack of mobile Creator wallpapers.Just getting started or wanting to learn MySQL? Find out about my premium blog posts and MySQL Beginner Series here.

The Newsletter for PHP and MySQL Developers

Disclosure: Some of the services and products links in this post are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

The post How to Set Up MySQL in a Docker Container appeared first on Digital Owl’s Prose.