Docker: create a MariaDB or MySQL container

In this tutorial, I will explain how to create a MariaDB or MySQL container with Docker.

Today, more and more applications are present in businesses and require a database.

Previously, we often had a server dedicated to the MySQL or MariaDB database where all the databases coexisted on the same server and with the same service. To “isolate”, we had to create users dedicated to each database.

The problem with this solution is sharing the same service; if an application made a request to crash the database server, all the applications were impacted.

With Docker, we will be able to run several different instances on the same server and isolate MariaDB and/MySQL servers.

This solution also gives us flexibility on versions, because certain publishers want specific versions.

To start, you must have a Linux server with Docker and docker-compose installed, if you need I covered this subject in the following tutorial: Install Docker on Ubuntu and Debian.

In order to facilitate the deployment of MariaDB and/or MySQL database, I created a set of docker-compose.yml and .env files, which can be reused at this address : https://git.rdr-it.com/docker/mariadb-or-mysql-container

To illustrate the tutorial and the use of files, we will deploy two database containers, a MariaDB and a MySQL.

Before you start, you need to know the versions you need and retrieve the version tags:

On the server, create a folder that will receive the container:

sudo mkdir mariadb-c1

For my part, I usually create a containers folder at the root and then I create a subfolder per container.

Place in the folder you just created and clone the repository there:

sudo git clone https://git.rdr-it.com/docker/mariadb-or-mysql-container.git .

Once the repository has been cloned, we will edit the .env file which contains all the container customization parameters.

sudo nano .env

Here is the list of parameters to edit:

ParamètreValeur
NAME_CONTAINERNom du conteneur
DB_ENGINEmariadb ou mysql
DB_VERSIONTag de l’image que vous, souhaitez pour le moteur de base de données
PORT_EXPOSEPour depuis lequel le service MySQL ou MariaDB sera disponible
MYSQL_DATABASENom de la base de données qui sera créé
MYSQL_ROOT_PASSWORDMot de passe du compte root
MYSQL_USERUtilisateur qui sera créé en même temps de que la base de données
MYSQL_PASSWORDMot de passe de l’utilisateur MYSQL_USER

This is what the file looks like to have a MariaDB container in version 10.9 with a db1 database, the container will be exposed on port 40001 of the host server.

All that remains is to start the container:

sudo docker-compose up -d

If the image is not present, it will first download it and then start the container. We could also have previously used the sudo docker-compose pull command to download the image to the Docker server.

The sudo docker ps command allows you to see that our container is working:

We start the operation again, this time to have a MySQL 5.7 container:

Now, we can see that I have two containers (servers) MariaDB and MySQL running on the same server:

If you need to restore a database to a container, here is the command:

sudo docker exec -i <container-name> mysql --user <user> --password=<password-for-user> <database> < </home/my-user/backup-file.sql>

You now know how to deploy MariaDB and MySQL containers on the same server.

In the context of container use, the MySQL / MariaDB server is automatically available for remote access.




Leave a Comment