how to install and configure mariadb on docker

How To Use and Manage MariaDB On Docker

Let’s say at one time we were asked to provide MariaDB which is used for testing an application, but at that time we were asked not to use existing mariaDB. And this new MariaDB must be isolated from the ongoing development environment. Whereas on the other hand, the resources we have are very limited even for deploying VMs, we are a little hesitant to do so. The best solution we can take from this situation is to using Docker, because Docker requires a very small amount of resources. It can run on a virtualized system. It is used both in development and in production environments. At this article we will discuss how to Use and manage MariaDB on Docker.

This article will focus on “how to use and manage of MariaDB on Docker”, while the discussion on the Docker installation itself has been discussed in the previous article. The contents of this article are:

  1. Prerequisite
  2. Using MariaDB, will describe how to select and download MariaDB repository for Docker
  3. Creating Container
  4. MariaDB management on container

1. Prerequisite

As a set of PaaS (Platform as a Service), Docker uses OS-level virtualization to deliver software in packages called containers. So if we want to use Docker we have to install it first and running on Operating system. At this case the Docker installation was discussed on previous article, How To Install Docker Community Edition (CE) on CentOS 7. We use the latest Docker version (version 19.03.5).

2. Using MariaDB Images

The easiest way to use MariaDB on Docker is choosing a MariaDB image and creating a container, then we configure and manage our new MariaDB inside Docker. At this section, it will be explaine the step by step as below .

2.1. Search MariaDB image

Searching MariaDB image in the Docker Hub (the official set of repositories) and select the matched one with our requirement, using command below.

[ramans@localhost ~]$ docker search mariadb

If we have found an image that we want to use, we can download it via Docker. Some layers including necessary dependencies will be downloaded too. Note that, once a layer is downloaded for a certain image, Docker will not need to download it again for another image. At this case we will use image name as mariadb, the official MariaDB image, marked as Official [Ok] as image above.

2.2. Install the default MariaDB image

After we have selected the MariaDB image, then we install it to enable it running on Docker. The command we use is as below.

[ramans@localhost ~]$ docker pull mariadb

After installation was completed done, we verify the docker image that we have created befor. It must be a new image that is ready to be used in container. To query the image, use the below :

[ramans@localhost ~]$ docker images
 REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
 mariadb             latest              38c637f60d24        6 days ago          355MB

3. Creating a Container

After installing MariaDB image was done, the next step is to create the container base on the image, as we know an image is not a running process; it is just the software needed to be launched . Creating container will be as follow (in this article, the container name is mariadbdock_01)

[ramans@localhost ~]$ docker run --name mariadbdock_01 -e MYSQL_ROOT_PASSWORD=mypass -d mariadb

We can verify the container that we have created, the command as below, We should get an output similar to this one:

[ramans@localhost ~]$ docker ps
 CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
 59660274ce36        mariadb             "docker-entrypoint.s…"   About a minute ago   Up About a minute   3306/tcp            mariadbdock_01

Remember about the password parameter above, because the password will be use for MariaDB management in the next steps.

4. MariaDB Management on Container

Until now, we have create container and ready to be used. To access the container via Bash, we can run this command:

[ramans@localhost ~]$ docker exec -it mariadbdock_01 bash

When we enter the container, we will have an unique ID. At our environment the unique ID is 59660274ce36, and the prompt will be root@59660274ce36:/#, because we have a root privilege on the container. Now we can use normal Linux commands like cd, ls, etc. , for example:

Finally, we reached the time when we will use MariaDB at container as below :

root@59660274ce36:/# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.4.10-MariaDB-1:10.4.10+maria~bionic binary distribution

We can do any tasks as in the normal environment MariaDB, such like create database or create tables on MariaDB as below.


Installing MariaDB on Docker and create the container based its image has been successful done. Docker is useful platform for developing and testing an applications.

Share this article via :

Leave a Reply

Your email address will not be published. Required fields are marked *