Docker makes it easy to set up MariaDB quickly. First, download the appropriate image. Then configure and start the container with your preferred settings. This lets you work in a controlled environment, independent of your computer’s main system.

What are the advantages of running MariaDB in Docker?

Docker provides a straightforward way to run MariaDB in isolated environments. You can start new MariaDB containers quickly and try different versions without them interfering with each other. This saves time, avoids conflicts with local installations and ensures consistent conditions for development and testing.

What are the requirements?

Before getting started, make sure you have:

  • Docker is installed and running on your system
  • Access to a command line (Terminal, PowerShell, etc.) with administrator rights
  • Optional for MariaDB: Docker Compose for more complex setups
Managed Database Services
Time-saving database services
  • Enterprise-grade architecture managed by experts
  • Flexible solutions tailored to your requirements
  • Leading security in ISO-certified data centers

How to install MariaDB in Docker

Below, we walk you through installing MariaDB in Docker in just a few steps. For details on setting up Docker itself, see our Docker Tutorial.

Step 1: Install Docker

If you haven’t installed Docker yet, use the official installation script:

curl -sSL https://get.docker.com/ | sh
bash

For Windows or macOS, Docker Desktop is recommended. After installation, check this with:

docker --version
bash

You should see a version number such as Docker version 27.5.1, build cb74dfcd. This means Docker has been installed correctly.

Step 2: Download MariaDB image

Now download the official MariaDB Docker image from Docker Hub. This is a ready-to-use version of MariaDB:

docker pull mariadb:latest
bash

To get a specific version, use:

docker pull mariadb:10.11
bash

You can then check all locally stored images with the following command:

docker images
bash

Look for mariadb and the version tag in the list.

Step 3: Start the MariaDB Docker container

Create the container, using the following command:

docker run --name mariadb-container -e MYSQL_ROOT_PASSWORD=password -d -p 3306:3306 mariadb:latest
bash
  • --name mariadb-container: Gives the container a name, so you can refer to it later.
  • -e MYSQL_ROOT_PASSWORD=password: Sets the root password for the MariaDB database. You will need this later to log in.
  • -d: Runs the container in the background (in detached mode), so your console remains free.
  • -p 3306:3306: Makes the database accessible on your computer through port 3306.

After running this command, Docker should start the container. To check the status, enter:

docker ps
bash

In the list of running containers, mariadb-container should appear with the status Up. If the container isn’t running, view its log files for troubleshooting with:

docker logs mariadb-container
bash

The log output helps you identify any configuration errors.

Step 4: Access MariaDB

Once the MariaDB Docker container is running, you can connect with a MySQL-compatible client, such as the MySQL command line tool or graphical tools like DBeaver, HeidiSQL or Beekeeper Studio.

For example, from the command line:

mysql -h 127.0.0.1 -P 3306 -u root -p
bash

Enter the password you set with MYSQL_ROOT_PASSWORD.

If successful, you’ll have access to the MariaDB console and can run SQL commands such as:

SHOW DATABASES;
sql

This will list the default databases. You can then create your own databases and tables.

Compute Engine
The ideal IaaS for your workload
  • Cost-effective vCPUs and powerful dedicated cores
  • Flexibility with no minimum contract
  • 24/7 expert support included

Step 5: Use volumes to keep data between container runs

By default, container data is deleted when the container is removed. To secure your data, use a volume:

docker run --name mariadb-container -e MYSQL_ROOT_PASSWORD=password -d \
    -v mariadb_data:/var/lib/mysql \
    -p 3306:3306 mariadb:latest
bash

Here -v mariadb_data:/var/lib/mysql creates a Docker volume named mariadb_data for storing MariaDB data.

You can view the stored volumes at any time using this command:

docker volume ls
bash

And use this command to inspect a volume:

docker volume inspect mariadb_data
bash

Step 6: Manage MariaDB with Docker Compose

For more complex setups, you should use Docker Compose. First create a file named docker-compose.yml and add the following content:

version: '3.1'
services:
    mariadb:
        image: mariadb:latest
        container_name: mariadb-compose
        environment:
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: example_db
        ports:
            - "3306:3306"
        volumes:
            - mariadb_data:/var/lib/mysql
volumes:
    mariadb_data:
yaml

Then, start the environment with:

docker-compose up -d
bash

MariaDB will run in the background, with saved data and a preconfigured database.

Step 7: Stop, start and remove container

You can manage the MariaDB Docker container at any time. If you’ve set up a volume, you can stop or restart the container without losing your database files.

Stop container:

docker stop mariadb-container
bash

Start container:

docker start mariadb-container
bash

Remove container:

docker rm mariadb-container
bash

Remove container and its volume:

docker rm -v mariadb-container
bash

Step 8: Enable automatic

If you want your MariaDB Docker container to start automatically whenever your computer restarts, use the --restart option when creating it:

docker run --name mariadb-container -e MYSQL_ROOT_PASSWORD=password \
    -v mariadb_data:/var/lib/mysql \
    -p 3306:3306 \
    --restart unless-stopped \
    -d mariadb:latest
bash

--restart unless-stopped tells Docker to start the container automatically after a reboot, unless you’ve stopped it manually.

Was this article helpful?
Go to Main Menu