In this guide we are going to explore how to run Mariadb 10 locally with docker and docker compose. This can be helpful if you want to run Mariadb 10 locally without installing it in your machine or if you want to run multiple versions of Mariadb seamlessly.
Related content
- How to run Mysql 8 with Docker and Docker-Compose
- How to run MsSQL Server 2019 with Docker and Docker-Compose
- Running Postgresql 14 with Docker and Docker-Compose
- How to run Redis 6 with Docker and Docker-Compose
- How to run Cassandra 4 with Docker and Docker-Compose
Prerequisites
To follow along, ensure you have the following:
- Docker installed locally and permissions to use it to launch containers
- Docker compose is installed locally
- Basic knowledge of the command line
Table of content
- Using the
docker run
command - Using the
docker-compose
tool
1. Using the docker run command
We are going to use the docker run
command to achieve our goal. The version of Mariadb that we want is mariadb:10.7
– the latest version of mariadb image.
Create data dir
mkdir -p ~/apps/mariadb/data
Run the container
$ 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:10.7
Unable to find image 'mariadb:10.7' 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:10.7
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 port3306
is mapped to host port3306
. That means the service can be accessed throughlocalhost: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 runmysqld
with a specific UID/GID. - The
-e
argument is for the environment variables. The supplied environment variables will be used to set up a Mariadb user, password and a database.
To check that our container is running as expected, use the docker ps
command:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75c4a2f716d9 mariadb:10.7 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp my-mariadb
In my case the container is running as my-mariadb
the name we gave it. We can login to the container using the docker exec
command while executing /bin/bash
interactively. Here we are also logging in to posgtres with the credentials we specified above and checking the version.
$ docker exec -it my-mariadb /bin/bash
groups: cannot find name for group ID 1000
[email protected]:/$ 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:
❯ docker stop my-mariadb
my-mariadb
➜ docker rm my-mariadb
my-mariadb
2. Using the docker-compose tool
We can achieve the same functionality with docker-compose
. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.
Docker Compose allows you to define the service (Mariadb in our case) with properties like the image
to use, ports
to expose, volumes
to mount and environment
variables.
Here is how we would use docker-compose
to achieve the functionality above. Save this as docker-compose.yaml
:
version: '3.9'
services:
mariadb:
image: mariadb:10.7
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
Now bring up the containers:
$ 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
Verify the container processes using the ps command:
$ 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 login to the container and login to Mariadb, use this:
$ docker-compose exec mariadb /bin/bash
[email protected]:/# 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)]>
Conclusion
In this guide we managed to run Mariadb 10 as a container in our system, we explored using the docker run
command while passing the required arguments an alternative approach of simplifying the process with docker-compose
4 Comments
Pingback: Links 12/11/2021: PipeWire 0.3.40 and FreeBSD 12.3 RC1 | Techrights
Pingback: How to backup and restore databases in Mysql or Mariadb
Pingback: How to run RethinkDB with Docker and Docker-Compose
Thanks for great guide, I have question please.
I have huge dedicated server and i need to setup multiple dockers on same server to create MariaDB cluster, is that possible using your guide.
Cheers