Main Tutorials

Docker – Running MariaDB as a container

This article shows how to use Docker to run MariaDB as a container.

Tables of contents:

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.

Terminal

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.

Terminal

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.

Terminal

  docker exec -it some-mariadb mariadb -uroot -p
Terminal

$ 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:

Terminal

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.

Terminal

  -v /path/to/mariadb-data:/var/lib/mysql

5. Access the MariaDB container log

Terminal

  docker logs some-mariadb

6. Stop the MariaDB Container

Terminal

  docker stop some-mariadb

7. Start the MariaDB Container again

Terminal

  docker start some-mariadb

8. Removing the MariaDB Container

Terminal

  docker rm some-mariadb

9. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments