How to deploy a MySQL server using docker containers

pauladj

Paula

Posted on March 27, 2023

How to deploy a MySQL server using docker containers

Docker has revolutioned the way we deploy and manage applications. One common use case is running databases like MySQL in Docker containers for development, testing, or even production environments.

This article aims to explain how to deploy and configure a MySQL server using docker containers.

We will go step by step, and at the end of this blog, you will be able to access your MySQL server using a MySQL client, such as HeidiSQL.

Why containers?

Imagine you are working on multiple projects with different needs. One of them needs a MySQL 8.0.32 and the other the version 5.7. How would you go about it?

Maybe your first thought is to install them locally or to use XAMPP, but for starters, you would need to change the default port.

And if you want to have two separate MySQL with the same version?

Things can get complicated quickly.

With containers though, it’s easy. You specify the version and you are done.

Install Docker

If you haven’t already, start by installing Docker on your system. You can download Docker Desktop for Windows and macOS or Docker Engine for Linux from the official Docker website.

It’s also available for the new M1/M2 macOS with the arm64 architecture.

Docker image

The first step is to find the MySQL base image we want to use. DockerHub is a good place to start. If you type "mysql" in the search bar you can choose the image you want, "mysql" or "mariadb" from the results.

In this case, we are choosing mysql:8.0.

The link to the image: https://hub.docker.com/_/mysql

Docker compose

We could execute a docker run and start the container quickly. If we want to change and restart its configuration it’s easier to use a docker-compose.yml file.

version: "3.9"
services:
  database:
    container_name: "database"
    image: mysql:8.0
    environment:
      - MYSQL_ROOT_PASSWORD=dummy_pass
    volumes:
      - db-data:/var/lib/mysql
    ports:
      - "3306:3306"
volumes:
  db-data:
Enter fullscreen mode Exit fullscreen mode

You can find all the possible environment variables on the docker image page.

The next step is to execute the following command:
docker-compose up -d

After that, we can connect to the MySQL container using a MySQL client like HeidiSQL.

Image description

You can access the mysql server from the terminal too. For example:

docker exec -it database /bin/bash -c "mysql -uroot -pdummy_pass"

Image description

Persist data

By default, Docker containers are ephemeral, meaning data is lost when the container stops. To persist your MySQL data, you can use Docker volumes.
For example, in the docker-compose example we have db-data:/var/lib/mysql. That is a docker volume.


Running MySQL in Docker containers provides flexibility and isolation for your database environment. It’s a convenient way to manage MySQL for various use cases, from development to production.

💖 💪 🙅 🚩
pauladj
Paula

Posted on March 27, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related