Running a MariaDB Server using Docker

In this tutorial, we will show you how to run a MariaDB Server using Docker Compose.

MariaDB Docker Compose

MariaDB is one of the most popular SQL relational database management systems. It is well regarded as a mostly drop-in replacement for MySQL.

We won’t be covering the significant differences between MariaDB, MySQL, and PostgreSQL in this article, but it is well worth exploring each one to see which best suits your needs. Most users will be fine with any of the three major relational databases.

There are numerous ways to set up and run a MariaDB server, one of the easiest being to utilize Docker. Using Docker to run MariaDB has several advantages. One is that it allows you to install and run a specific version of the database server without it being available on your operating system. Additionally, the benefit of using a container is that it is isolated from the rest of your operating system.

By the end of this guide, you will have a MariaDB server up and running within its own Docker container using Compose. We chose to use Compose because it makes container management significantly easier and is great if you have several services you want to run together.

Using Docker to Run MariaDB

Over the following few sections, we will walk you through getting MariaDB to run on your system using Docker.

We will even show you a quick and easy way to install the Docker runtime if you don’t already have it. The following steps will be within the terminal and should work on almost any Linux system that can run Docker.

The MariaDB team provides Docker containers of their server for most architectures. You can follow this guide on ARM devices like the Raspberry Pi.

Installing Docker on your System

1. Before you can set up a MariaDB Docker container, you must install Docker itself. If you already have Docker installed, skip to step two of this tutorial.

Alternatively, if you haven’t installed the Docker run time, you only need to follow our Linux Docker installation guide.

https://pimylifeup.com/linux-docker-install/

Creating a Folder to Store your MariaDB Docker Compose File

2. Our first set-up step requires us to create a directory where we will store the Docker Compose file for MariaDB. Keeping this file and its data in its own directory makes it easier to manage.

You can create a directory at “/opt/stacks/mariadb/” using the following mkdir command. We use the “-p” option so the tool will create all missing directories.

sudo mkdir -p /opt/stacks/mariadb

3. With your new directory created, you will want to change into it by using the cd command.

We need to be in this directory to run the Compose file we are writing.

cd /opt/stacks/mariadb

Writing a Docker Compose File for MariaDB

4. Once you are in your newly created directory, we can move on to writing a Docker Compose file for MariaDB. Docker uses a Compose file to store instructions. It follows these instructions to start and maintain your container.

By running the following command, you can begin writing this Compose file using the nano text editor.

sudo nano compose.yaml

5. Within this file, you will want to type in the following lines. While filling out this file, replace the following placeholders.

  • <ROOTPASSWORD>: use this placeholder to specify a password that will be used for the root MariaDB user. This user has full control over your database server, so ensure you set something secure.
  • <DATABASENAME>: Using this environment variable, you can specify a database to be created when your MariaDB Docker container starts.
  • <USERNAME>: You can also create a user that will be automatically assigned permissions for your database.
  • <USERPASSWORD>: This final placeholder is where you will set a password for your user. Like the root user, you should keep this password as something secure.

Additionally, if you want to use a particular version of MariaDB, replace “latest” with the version number.

services:
  db:
    image: mariadb:latest
    environment:
      MYSQL_ROOT_PASSWORD: <ROOTPASSWORD>
      MYSQL_DATABASE: <DATABASENAME>
      MYSQL_USER: <USERNAME>
      MYSQL_PASSWORD: <USERPASSWORD>
    volumes:
      - ./data:/var/lib/mysql
    ports:
      - "3306:3306"

6. After filling out the Compose file, you can save and quit by pressing CTRL + X, Y, and then ENTER.

Starting your MariaDB Server

7. Now that the Docker Compose file has been written, you can use the command below to start up your MariaDB server.

Using the “-d” option will cause Docker to detach from the terminal once the database server starts up.

docker compose up -d

8. Now, you should be able to connect to your new MariaDB server using port “3306“. If you are connecting to the server from the same machine, you can simply use “localhost” or “127.0.0.1” to connect.

If you are running this container on a device exposed to the internet, ensure a firewall is enabled on your system.

Updating your MariaDB Server using Docker Compose

One advantage of using a Docker container to run the MariaDB server on your device is that it makes updating the server incredibly easy.

You will have a brief downtime during the update process as your database server is stopped and started again.

1. Before you update your MariaDB container, you will want to change to the directory we created at the start of this guide.

You can swap to the correct directory by running the following command.

cd /opt/stacks/mariadb

2. After swapping to the correct directory, you must tell Docker to pull the latest version of the MariaDB image.

You can get Docker to do this using the following command within the terminal.

docker compose pull

3. Once a new image is downloaded, Docker will not automatically move your running container to it.

Instead, you must either restart your container or use the following command. The command below will check over your Compose file, detect the new image available for MariaDB, and then restart the container using it.

docker compose up -d

Conclusion

At this stage in the tutorial, you should now have a MariaDB database server running on your machine using Docker.

Docker is a great way to manage database servers like MariaDB. It can be started incredibly fast and restarted or updated. It also means you can easily use the exact version of MariaDB that you require rather than being stuck with the one available on your operating system.

Please feel free to leave a comment below if you have any questions or concerns about using Docker to run MariaDB.

If you found this guide helpful, please check out our many other Docker tutorials.

Leave a Reply

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