Docker – Running MariaDB as a container
This article shows how to use Docker to run MariaDB as a container.
Tables of contents:
- 1. Run MariaDB as a container
- 2. Specify the MariaDB version
- 3. Access the running MariaDB container
- 4. MariaDB Persistent Storage with Volume
- 5. Access the MariaDB container log
- 6. Stop the MariaDB Container
- 7. Start the MariaDB Container again
- 8. Removing the MariaDB Container
- 9. References
Technologies used:
- Docker 24.0.5
- Official Docker
mariadb
image (version 11.1.2, latest)
1. Run MariaDB as a container
The below command runs MariaDB as a container.
docker run --name some-mariadb -p 8888:3306 -e MARIADB_ROOT_PASSWORD=password -d mariadb
The command breakdown:
docker run
: Create and run the container.--name some-mariadb
: Set the container’s name.-p 8888:3306
: Set the port mapping. MariaDB database defaults expose port 3306, and this command maps the local port 8888 to the container port 3306 (inside Docker).-e
: Set the environment variables.-e MARIADB_ROOT_PASSWORD=password
: Set the password for the root user.-d
: Run the container in the background.mariadb
: This is the image name we want to pull from the Docker Hub (default). By default, the latest tag is used, which will get the latest version of MariaDB.
2. Specify the MariaDB version
We can use mariadb:tag
to specify the version of MariaDB we want to run as a container. By default, the latest tag is used, which will get the latest version of MariaDB.
The image mariadb:10.11.5
will run MariaDB version 10.11.5.
docker run --name some-mariadb -e MARIADB_ROOT_PASSWORD=password -d mariadb:10.11.5
Note
We can find a list of all available tags on the MariaDB Docker Hub page.
3. Access the running MariaDB container
We can use docker exec
to access the maridb
shell inside the running MariaDB container.
docker exec -it some-mariadb mariadb -uroot -p
$ docker exec -it some-mariadb mariadb -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 11.1.2-MariaDB-1:11.1.2+maria~ubu2204 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() |
+---------------------------------------+
| 11.1.2-MariaDB-1:11.1.2+maria~ubu2204 |
+---------------------------------------+
1 row in set (0.000 sec)
MariaDB [(none)]> exit
Bye
4. MariaDB Persistent Storage with Volume
If we want to keep the data persistent (so that data isn’t lost when the container is stopped/removed), use Docker volumes -v
.
4.1 Create a directory /path/to/mariadb-data
.
4.2 Start the MariaDB container like this:
docker run --name some-mariadb -v /path/to/mariadb-data:/var/lib/mysql -e MARIADB_ROOT_PASSWORD=password -d mariadb
The -v /path/to/mariadb-data:/var/lib/mysql
command mounts the local host /path/to/mariadb-data
directory to the /var/lib/mysql
directory which inside the running MariaDB container.
P.S. MariaDB default will write its data to /var/lib/mysql
.
4.3 Now, the MariaDB stores all its data in the local /path/to/mariadb-data
directory. We will still find the data if we remove the container and start a new one using the same volume.
-v /path/to/mariadb-data:/var/lib/mysql
5. Access the MariaDB container log
docker logs some-mariadb
6. Stop the MariaDB Container
docker stop some-mariadb
7. Start the MariaDB Container again
docker start some-mariadb
8. Removing the MariaDB Container
docker rm some-mariadb
Further Reading
How to run an init script for Docker MariaDB