How to Run MariaDB with Docker and Docker Compose (Step-by-Step)

Run MariaDB in Docker with docker run or docker-compose. Persistent data volumes, MYSQL_ROOT_PASSWORD, MYSQL_USER, MYSQL_DATABASE, and connect on port 3306. Includes docker-compose.yaml example.

MariaDB is a popular open source, MySQL-compatible relational database. Running MariaDB in Docker (or with Docker Compose) lets you use it locally without installing it on the host, run multiple versions side by side, and keep data in a host volume so it survives container restarts. This guide shows how to run MariaDB with Docker using both docker run and a docker-compose.yaml with environment variables for root password, user, and database.

In this guide you’ll:

  • Run the official mariadb image with docker run, mapping port 3306 and mounting a data volume
  • Set up MYSQL_ROOT_PASSWORD, MYSQL_USER, MYSQL_PASSWORD, and MYSQL_DATABASE via environment variables
  • Run the same setup with Docker Compose using a docker-compose.yaml
  • Connect to MariaDB inside the container and verify the version

Related database-in-Docker guides:

Prerequisites

To follow along, ensure you have the following:

  1. Docker installed locally and permissions to use it to launch containers
  2. Docker compose is installed locally
  3. Basic knowledge of the command line

Using the docker run command

We’ll use the docker run command with the official MariaDB image. We use mariadb:latest here; you can pin a version (e.g. mariadb:10.11) for production.

Create a data directory (so database files persist on the host):

1
mkdir -p ~/apps/mariadb/data

Run the container:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ docker run -d \
    --name my-mariadb \
    -p 3306:3306 \
    -v ~/apps/mariadb/data:/var/lib/mysql \
    --user 1000:1000 \
    -e MYSQL_ROOT_PASSWORD=S3cret \
    -e MYSQL_PASSWORD=An0thrS3crt \
    -e MYSQL_USER=citizix_user \
    -e MYSQL_DATABASE=citizix_db \
    mariadb:latest

Unable to find image 'mariadb:latest' locally
10.7: Pulling from library/mariadb
7b1a6ab2e44d: Pull complete
034655750c88: Pull complete
f0b757a2a0f0: Pull complete
4bbcce26bc5e: Pull complete
04f220ee9266: Pull complete
89c8a77f7842: Pull complete
d1de5652303b: Pull complete
e10058b6c45e: Pull complete
a07ac6f8b619: Pull complete
6260e28f2886: Pull complete
Digest: sha256:832c6e488f49720f484f87ee9f2cd4487321b373db07ac77037860bcd97d92bb
Status: Downloaded newer image for mariadb:latest
75c4a2f716d9ec95fe7ca7a3c7b071755a9ec94f824e0aa17f703d831aedadd8

In the above command:

  • The -d instructs docker container to run as a detached process. It run container in background and print container ID
  • -p is for port mapping. We are instructing the container to expose the container port externally. Container port 3306 is mapped to host port 3306. That means the service can be accessed through localhost:3306
  • The -v directive is used to mount volumes. In our case we are mounting the container volume /var/lib/mysql to host path ~/apps/mariadb/data. Containers are ephemeral devices that will contain its data for the time it is running. Once a container is stopped, its data is lost. Mounting volumes ensures that the data is added to a host path that can be reused when the container is restarted.
  • The --user argument is used to run the container with an arbitrary user (non root user). This happens if you need to run mysqld with a specific UID/GID.
  • The -e argument sets environment variables. The official MariaDB image uses them to create the root password, a database, and an optional user (e.g. MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE).

To check that our container is running as expected, use the docker ps command:

1
2
3
4
$ docker ps

CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                                       NAMES
75c4a2f716d9   mariadb:latest   "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   my-mariadb

The container is running with the name my-mariadb. To open a shell inside it and connect to MariaDB with the user we created, use docker exec and then run mysql with the credentials you set via environment variables:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ docker exec -it my-mariadb /bin/bash

citizix@75c4a2f716d9:/$ mysql -u citizix_user -pAn0thrS3crt

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.7.1-MariaDB-1:10.7.1+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select version();
+-------------------------------------+
| version()                           |
+-------------------------------------+
| 10.7.1-MariaDB-1:10.7.1+maria~focal |
+-------------------------------------+
1 row in set (0.000 sec)

MariaDB [(none)]>

If you need to clean up the container when not in use, you can stop and remove the container using this command:

1
2
3
4
5
6
7
$ docker stop my-mariadb

my-mariadb

$ docker rm my-mariadb

my-mariadb

Using Docker Compose

You can run the same MariaDB setup with Docker Compose: define the service in a YAML file (image, ports, volumes, environment) and start it with docker compose up -d or docker-compose up -d.

Save the following as docker-compose.yaml in a directory (e.g. where you created ~/apps/mariadb). The volume path in the example uses ~/apps/mariadb; for the first run you can use ~/apps/mariadb/data to match the docker run example if you already have data there.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
services:
  mariadb:
    image: mariadb:latest
    ports:
      - 3306:3306
    volumes:
      - ~/apps/mariadb:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=S3cret
      - MYSQL_PASSWORD=An0thrS3crt
      - MYSQL_USER=citizix_user
      - MYSQL_DATABASE=citizix_db

From that directory, start MariaDB:

1
2
3
4
5
docker compose up -d
# or: docker-compose up -d

Creating network "tmp_default" with the default driver
Creating tmp_mariadb_1 ... done

The commands:

  • up brings up the container
  • -d in a detached mode

Check that the container is running:

1
2
3
4
5
6
7
docker compose ps
# or: docker-compose ps

    Name                   Command              State               Ports
--------------------------------------------------------------------------------------
tmp_mariadb_1   docker-entrypoint.sh mariadbd   Up      0.0.0.0:3306->3306/tcp,:::3306
                                                        ->3306/tcp

To open a shell in the container and connect to MariaDB:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
docker compose exec mariadb /bin/bash
# or: docker-compose exec mariadb /bin/bash

root@07f9050cea89:/# mysql -u citizix_user -pAn0thrS3crt
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.7.1-MariaDB-1:10.7.1+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select version();
+-------------------------------------+
| version()                           |
+-------------------------------------+
| 10.7.1-MariaDB-1:10.7.1+maria~focal |
+-------------------------------------+
1 row in set (0.000 sec)

MariaDB [(none)]>

Frequently Asked Questions (FAQ)

What is MariaDB?

MariaDB is an open source, community-developed relational database that is a drop-in replacement for MySQL. It uses the same client protocol and is compatible with MySQL clients and drivers. The official MariaDB Docker image is maintained on Docker Hub.

What port does MariaDB use in Docker?

MariaDB listens on TCP port 3306 inside the container. Map it to the host with -p 3306:3306 (or in Compose: ports: - "3306:3306") so you can connect from the host or other containers via localhost:3306.

How do I persist MariaDB data in Docker?

Mount a host directory or named volume to /var/lib/mysql inside the container. For example: -v ~/apps/mariadb/data:/var/lib/mysql with docker run, or in Compose: volumes: - mariadb_data:/var/lib/mysql with a named volume. Without a volume, data is lost when the container is removed.

Can I use the same volume for docker run and Docker Compose?

Yes. If you created data with docker run using -v ~/apps/mariadb/data:/var/lib/mysql, use the same host path in your Compose file (e.g. ~/apps/mariadb/data:/var/lib/mysql) so the Compose stack uses that existing data. Ensure only one container is using the volume at a time.

Conclusion

You ran MariaDB in Docker using both docker run (with port 3306, volume, and environment variables) and Docker Compose with a docker-compose.yaml. Data is stored on the host so it survives restarts. For other databases in Docker, see MySQL 8, PostgreSQL 14, or Redis 6.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy