Creating and managing a MariaDB Docker container

You are viewing an old version of this article. View the current version here.

See MariaDB and Docker in action!

  • Set up web-based developer environments locally, and connect MariaDB to VS Code Server, CloudBeaver, PHP/Laravel and phpMyAdmin, using a single docker-compose command and configuration file.
Watch the Webinar

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.

S virtual machine would certainly serve the scope. However, this means to install 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 deamon 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.

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 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

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:

docker ps -a

Creating the 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 mariadb-10.0 -e MYSQL_ROOT_PASSWORD=mypass -d mariadb

mariadb-10.0 is the name we want to assign the container. If we don't specify a name, an id will be automatically generated.

Docker will responde 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:

docker ps
<</code>

We should get an output similar to this one:

<<code>>
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
819b786a8b48        mariadb             "/docker-entrypoint.   4 minutes ago       Up 4 minutes        3306/tcp            mariadb-10.0        

Running and stopping the container

The container can be stopped like this:

docker stop mariadb-10.0

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 mariadb-10.0

But 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 mariadb-10.0

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 mariadb-10.0 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.

Note that, if we run mysqladmin shutdown or the SHUTDOWN command to stop the container, the container will be deactivated, and we will automatically exit to our system.

Connecting MariaDB from outside

After modifying the my.cnf file, we will be able to connect MariaDB from outside the container. Even other Docker containers will be able to connect each others - 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).

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.