- Installing Docker on your system with the universal installation script
- Using MariaDB images
- Downloading an image
- Creating a container
- Running and stopping the container
- Troubleshooting a container
- Accessing the container
- Connecting to MariaDB from outside
- Installing MariaDB on another image
- See also
Sometimes we want to install a specific version of MariaDB, MariaDB Galera Cluster, or MaxScale on a certain system, but no packages are available. Or maybe, we simply want to isolate MariaDB from the rest of the system, to be sure that we won't cause any damage.
A virtual machine would certainly serve the scope. However, this means installing a system on the top of another system. It requires a lot of resources.
In many cases, the best solution is Docker. Docker is a framework that runs containers. A container is meant to run a specific daemon, and the software that is needed for that daemon to properly work. Docker does not virtualize a whole system; a container only includes the packages that are not included in the underlying system.
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.
Docker is an open source project, released under the Apache License, version 2.
Note that, while your package repositories could have a package called
docker, it is probably not the Docker we are talking about. The Docker package could be called
Installing Docker on your system with the universal installation script
The script below will install the Docker repositories, required kernel modules and packages on the most common Linux distributions:
curl -sSL https://get.docker.com/ | sh
Using MariaDB images
The easiest way to use MariaDB on Docker is choosing a MariaDB image and creating a container.
Downloading an image
You can download a MariaDB image for Docker from the official repository, or choose another image that better suits your needs. You can search Docker Hub (the official set of repositories) for an image with this command:
docker search mariadb
Once you have found an image that you want to use, you 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.
For example, if you want to install the default MariaDB image, you can type:
docker pull mariadb:latest
This will install the latest version.
:latest is the default tag, and it can be omitted. We can also replace it with a specific version, such as
You will see a list of necessary layers. For each layer, Dockers will say if it is already present, or its download progress.
To get a list of installed images:
Creating a container
An image is not a running process; it is just the software needed to be launched. To run it, we must create a container first. The command needed to create a container can usually be found in the image documentation. For example, to create a container for the official MariaDB image:
docker run --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -d mariadb
mariadbtest is the name we want to assign the container. If we don't specify a name, an id will be automatically generated.
You can specify an optional major or minor version number - if you don't, the latest release will be used:
docker run --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -d mariadb:10.1
docker run --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -d mariadb:10.0.20
Optionally, after the image name, we can specify some options for mysqld. For example:
docker run --name mariadbtest -e MYSQL_ROOT_PASSWORD=mypass -d mariadb --log-bin --binlog-format=MIXED
Docker will respond with the container's id. But, just to be sure that the container has been created and is running, we can get a list of running containers in this way:
We should get an output similar to this one:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 819b786a8b48 mariadb "/docker-entrypoint. 4 minutes ago Up 4 minutes 3306/tcp mariadbtest
Running and stopping the container
Docker allows us to restart a container with a single command:
docker restart mariadbtest
The container can also be stopped like this:
docker stop mariadbtest
The container will not be destroyed by this command. The data will still live inside the container, even if MariaDB is not running. To restart the container and see our data, we can issue:
docker start mariadbtest
docker stop, the container will be gracefully terminated: a
SIGTERM signal will be sent to the
mysqld process, and Docker will wait for the process to shutdown before returning the control to the shell. However, it is also possible to set a timeout, after which the process will be immediately killed with a
SIGKILL. Or it is possible to immediately kill the process, with no timeout.
docker stop --time=30 mariadbtest docker kill mariadbtest
In case we want to destroy a container, perhaps because the image does not suit our needs, we can stop it and then run:
docker rm mariadbtest
Note that the command above does not destroy the data volume that Docker has created for /var/lib/mysql. If you want to destroy the volume as well, use:
docker rm -v mariadbtest
A container can also be frozen with the
pause command. Docker will freeze the process using croups. MariaDB will not know that it is being frozen and, when we
unpause it, MariaDB will resume its work as expected.
unpause accept one or more container names. So, if we are running a cluster, we can freeze and resume all nodes simultaneously:
docker pause node1 node2 node3 docker unpause node1 node2 node3
Pausing a container is very useful when we need to temporarily free our system's resources. If the container is not crucial at this moment (for example, it is performing some batch work), we can free it to allow other programs to run faster.
Troubleshooting a container
If the container doesn't start, or is not working properly, we can investigate with the following command:
docker logs mariadbtest
This command shows what the daemon sent to the stdout since the last attempt of starting - the text that we typically see when we invoke
mysqld from the command line.
Accessing the container
Most MariaDB images, including the official one, have the skip-networking option set in their my.ini file. To use MariaDB we will need to edit the configuration file and restart the container.
To access the container via Bash, we will run this command:
docker exec -it mariadbtest bash
Now we can use normal Linux commands like cd, ls, etc. We will have root privileges. We can even install our favourite file editor, for example:
apt-get install vim
In some images, no repository is configured by default, so we may need to add them.
Connecting to MariaDB from outside
After modifying the my.cnf file, we will be able to connect to MariaDB from outside the container. Even other Docker containers will be able to connect to each other - for example, to form a Galera cluster.
There is a small caveat. When we try to connect to
localhost, the MariaDB library will try to use MariaDB's socket file in our system. But that file is in the container, so it will not be found. We will need to use 127.0.0.1 (IPv4) or ::1 (IPv6).
When running a cluster or a replication setup via Docker, we will want the containers to use different ports. The fastest way to achieve this is mapping the containers ports to different port on our system. We can do this when creating the containers (
docker run command), by using the
-p option, several times if necessary. For example, for Galera nodes we will use a mapping similar to this one:
-p 4306:3306 -p 5567:5567 -p 5444:5444 -p 5568:5568
Installing MariaDB on another image
It is possible to download a Linux distribution image, and to install MariaDB on it. This is not much harder than installing MariaDB on a regular operating system (which is easy), but it is still the hardest option. Normally we will try existing images first. However, it is possible that no image is available for the exact version we want, or we want a custom installation, or perhaps we want to use a distribution for which no images are available. In these cases, we will install MariaDB in an operating system image.
Daemonizing the operating system
First, we need the system image to run as a daemon. If we skip this step, MariaDB and all databases will be lost when the container stops.
To remonize an image, we need to give it a command that never ends. In the following example, we will create a Debian Jessie daemon that constantly pings the 220.127.116.11 special address:
docker run --name debian -p 3306:3306 -d debian /bin/sh -c "while true; do ping 18.104.22.168; done"
At this point, we can enter the shell and issue commands. First we will need to update the repositories, or no packages will be available. We can also update the packages, in case some of them are newer than the image. Then, we will need to install a text editor; we will need it to edit configuration files. For example:
docker exec -ti debian bash apt-get -y update apt-get -y upgrade apt-get -y install vim
Now we are ready to install MariaDB in the way we prefer.