How to deploy MySQL in Docker-containers

Containerum
Containerum
Published in
3 min readMar 16, 2018

Lots of web applications and services depend on databases. And most likely your business depends on these web applications and services. If you don’t want to setup a separate hardware server to host your DBs, consider using docker containers. Though it might not be the best idea for enterprise-scale applications, it is a good solution for companies running small and medium-sized instances in production. Especially if they don’t have a spare server for databases.

In this article I will show you how to deploy MySQL in Docker container accessible to another container or applications.

* The following instruction implies that you’ve already installed Docker (you can find the instructions here). I’m using Ubuntu 16.10 Server, but the steps below will be the same for any OS.

Docker pull

First, pull the right image. I’m using the latest MySQL version. If you need a specific release, change latest tag to the required version (5.5, 5.6, 5.7, 8.0). Now let’s pull the image:

docker pull mysql/mysql-server:latest

Now that you have MySQL image on your machine, you can deploy a container with

docker run — name=mysql01 -d mysql/mysql-server:latest

In this example I named the container mysql01, but you are free to name it as you wish.

Now we can check if the container with MySQL is running using docker ps.

The container with MySQL is running

MySQL password

When you setup MySQL on Linux, you’ll be asked to create an admin password right after installation. But here we have deployed a docker-container in which MySQL is already running. When you deploy MySQL in a container, it automatically generates a password. Let’s view it with docker logs $container_name.You may have to wait a minute before it is generated and included in the logs.

Note the password. You’ll need it later.

Login to MySQL server

Now login to the containerized server with docker exec -it mysql01 mysql -uroot -p. Enter the password from the logs — you can change it now with ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘newpassword’

Enter exit to quit MySQL interpreter. Now you can use the MySQL database in the container.

That’s it! If you don’t have the hardware to setup a separate server for a DB or just need a test environment, Docker makes deploying DB in containers extremely simple.

Hope you find this useful! Feel free to ask me anything, I’ll be glad to help. Also follow us on Twitter and join our Telegram chat to stay tuned!

Alexander Kirillov, Lead K8s Engineer at Containerum.

Disclaimer:
This is the beginning of a series of articles about Docker and Kubernetes by Containerum engineers. All tech and no ads. Stay tuned!

--

--

Containerum
Containerum

Containerum Platform for managing applications in Kubernetes.