{"id":6587,"date":"2020-08-03T22:37:02","date_gmt":"2020-08-03T19:37:02","guid":{"rendered":"https:\/\/kifarunix.com\/?p=6587"},"modified":"2024-03-14T22:46:46","modified_gmt":"2024-03-14T19:46:46","slug":"install-and-run-mariadb-as-a-docker-container","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-and-run-mariadb-as-a-docker-container\/","title":{"rendered":"Install and Run MariaDB as a Docker Container"},"content":{"rendered":"\n
Welcome to our basic tutorial on how to install and run MariaDB as a Docker container. According to Docker website<\/a>, “Docker is an open platform for developing, shipping, and running applications<\/em>“. A container<\/a> on the hand “is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings<\/em>“.<\/p>\n\n\n\n Before you can proceed, install Docker Engine on your system. Follow the links below to install Docker Engine on CentOS 8\/Ubuntu 20.04 systems;<\/p>\n\n\n\n Install and Use Docker CE on CentOS 8<\/a><\/p>\n\n\n\n Install Docker CE on Ubuntu 20.04<\/a><\/p>\n\n\n\n Every docker container is based off a specific image. Docker image contains everything that is necessary to run a container and is therefore a series of instructions that defines how an application is run.<\/p>\n\n\n\n You can build your own Docker image or simply utilize the images that the community have created. You can find the images at the Docker Hub<\/a>.<\/p>\n\n\n\n In this tutorial, we will be utilizing the community available Docker images.<\/p>\n\n\n\n First off, you need to identify the name of the specific MariaDB Docker Image. To search for the image on the docker hub, simply run the command below;<\/p>\n\n\n\n You will get a ton of MariaDB images that have been created;<\/p>\n\n\n\n Usually, an image with no prefix is considered official docker hub image, stable and being maintained. It also contains [OK]<\/strong> on the Official<\/strong> column. Therefore, we will pull the first image from the list above.<\/p>\n\n\n\n To download the Docker Image, you use the The command downloads the image to your system, if doesn’t already exist.<\/p>\n\n\n\n By default, when you pull a docker image, the latest version of it is downloaded. If you need to download other versions of MariaDB Docker Images, for example, MariaDB 10.3 Docker Image, simply use the command;<\/p>\n\n\n\n You can see a wide range of tags you can use on Docker Hub MariaDB<\/a> page.<\/p>\n\n\n\n You can list locally available images using the We have the latest MariaDB Docker Image downloaded.<\/p>\n\n\n\n Once you have the Docker image in place, you can then create and run a docker container based on the image. When running a MariaDB container, you need to specify one of following environment variables:<\/p>\n\n\n\n In our case here, the name of the image is, The command above basically creates and run (in the background, If you have other versions of MariaDB Docker Images, you can specify the tags to run a container based on that specific image. For example, assuming we had MariaDB 10.4 image and we would like to run a container based on that image, you could specify the tag as, Check Once you have run a container, you can list them using the To list all running and stopped containers, use To get just the running container IDs;<\/p>\n\n\n\n You can use Or<\/p>\n\n\n\n Your MariaDB docker container is now running in the background. You can access it by:<\/p>\n\n\n\n To launch an interactive bash shell for the MariaDB docker container, use the This takes you to container interactive bash shell;<\/p>\n\n\n\n You can as well login to your MariaDB container using mysql client on your host.<\/p>\n\n\n\n For this, you need to install MySQL client. For example, on Ubuntu systems, you can install MySQL client by running the command<\/p>\n\n\n\n Next, obtain the IP address assigned to your MariaDB container. To obtain an IP address of the container, use the In this case, our MariaDB container is assigned an IP address of 172.17.0.2<\/strong>.<\/p>\n\n\n\n To use the MySQL host client to connect to our container;<\/p>\n\n\n\n The command assumes that the container is listening on a default port, 3306.<\/p>\n\n\n\n You will need to use the password that you specified with the Within the shell, you can now administer your MariaDB as you would when running on a host.<\/p>\n\n\n\n NOTE: The IP address assigned to your docker container is dynamic and if you happen to restart the container, it will be assigned a new\/different IP address within the docker network.<\/p>\n\n\n\n If you have some external applications that needs to access your database running on MariaDB container, you need to expose the container port for external via the host system. By default, the container is only accessible within the host system<\/p>\n\n\n\n For example, our MariaDB container is listening on the default port 3306.<\/p>\n\n\n\n If you check your listening ports on your host for MariaDB port, you wont find any;<\/p>\n\n\n\n To expose a container port, use the If you need to use the same names for the container, just stop the existing container and remove it and relaunch new container using the same name.<\/p>\n\n\n\n Let us verify the port exposure<\/p>\n\n\n\n If you check on the PORTS<\/strong> column, you realize for our new container, the container port is exposed on port 3306 on the host and is listening on all interfaces;<\/p>\n\n\n\n If you need to specify the IP address of the host, use Checking the listening ports on the host again;<\/p>\n\n\n\n If you need to map the container port to a dynamic host port, use the option;<\/p>\n\n\n\n As you can see, the container port is exposed on dynamic port 32768.<\/p>\n\n\n\n You can always find what dynamic port the container port is mapped to using the When a container is removed, all if its data it had written to its internal volumes are removed as well. To ensure data is persistent across container deletes, you can map MariaDB Docker container internal volume to some host directory.<\/p>\n\n\n\n To bind an internal Docker volume to an external host volume, use the The default data directory for MariaDB container is For example, if I want the container data to be written to my host directory Therefore, any container data that is written to the container data directory, To stop a running container, use the You can stop all containers using their ids;<\/p>\n\n\n\n Once you are done with a container, you can choose to remove it;<\/p>\n\n\n\n To force removal;<\/p>\n\n\n\n To pause the container;<\/p>\n\n\n\n To restart;<\/p>\n\n\n\n To list docker volumes;<\/p>\n\n\n\n Find specific docker container volume using Remove specific volume;<\/p>\n\n\n\n Docker Documentation<\/a><\/p>\n\n\n\n MariaDB – Docker Hub<\/a><\/p>\n\n\n\nInstalling MariaDB as a Docker Container<\/h2>\n\n\n\n
Install Docker Engine<\/h3>\n\n\n\n
Download MariaDB Docker Image<\/a><\/h3>\n\n\n\n
Search for MariaDB Docker Image<\/a><\/h4>\n\n\n\n
docker search mariadb<\/code><\/pre>\n\n\n\n
NAME DESCRIPTION STARS OFFICIAL AUTOMATED\nmariadb MariaDB is a community-developed fork of MyS\u2026 3572 [OK]<\/strong> \nlinuxserver\/mariadb A Mariadb container, brought to you by Linux\u2026 151 \nbitnami\/mariadb Bitnami MariaDB Docker Image 120 [OK]\ntoughiq\/mariadb-cluster Dockerized Automated MariaDB Galera Cluster \u2026 41 [OK]\n...<\/code><\/pre>\n\n\n\n
Download MariaDB Docker Image from Docker Hub<\/a><\/h4>\n\n\n\n
docker pull <name-of-the-image><\/em><\/code> command. In this case, our image name is
mariadb<\/strong><\/code>.<\/p>\n\n\n\n
docker pull mariadb<\/code><\/pre>\n\n\n\n
docker pull mariadb:10.3<\/code><\/pre>\n\n\n\n
List Downloaded Docker Images<\/h4>\n\n\n\n
docker images<\/strong><\/code> command.<\/p>\n\n\n\n
docker images<\/code><\/pre>\n\n\n\n
REPOSITORY TAG IMAGE ID CREATED SIZE\nmariadb latest 8075b7694a2d 8 days ago 407MB<\/code><\/pre>\n\n\n\n
Running MariaDB Docker Container<\/a><\/h3>\n\n\n\n
docker run <options> <image-name><\/em><\/strong><\/code> is the Docker command for running Docker containers.<\/p>\n\n\n\n
\n
MYSQL_ROOT_PASSWORD<\/code><\/strong>: specifies the password that will be set for the MariaDB
root<\/code> (mandatory). <\/li>\n\n\n\n
MYSQL_ALLOW_EMPTY_PASSWORD<\/code><\/strong>: sets an empty password for MariaDB root user account (optional). Takes the values, yes or no<\/li>\n\n\n\n
MYSQL_RANDOM_ROOT_PASSWORD<\/code><\/strong>: Sets a random password for MariaDB root user account (optional). Takes the values, yes or no<\/li>\n<\/ul>\n\n\n\n
mariadb<\/code><\/strong> (with
latest<\/strong><\/code> being the tag<\/em>);<\/p>\n\n\n\n
docker run --name mariadbdemo -e MYSQL_ROOT_PASSWORD=password -d mariadb<\/code><\/pre>\n\n\n\n
-d<\/code><\/strong>) the MariaDB Docker container based on the latest image version available, sets the MariaDB database root password to password<\/strong> (
-e MYSQL_ROOT_PASSWORD=password<\/strong><\/code>) and the name of the container as
mariadbdemo<\/code><\/strong> (
--name mariadbdemo<\/code><\/strong>, random names are usually set for containers if you don’t specify the name).<\/p>\n\n\n\n
mariadb:10.4<\/strong><\/code>.<\/p>\n\n\n\n
docker run --name mariadbdemo -d -e MYSQL_ROOT_PASSWORD=password mariadb:10.4<\/code><\/pre>\n\n\n\n
docker run --help<\/code><\/strong> for more options.<\/p>\n\n\n\n
List Running Containers<\/a><\/h4>\n\n\n\n
docker ps<\/code> command.<\/p>\n\n\n\n
docker ps<\/code><\/pre>\n\n\n\n
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\nce03099cfffa mariadb \"docker-entrypoint.s\u2026\" 43 seconds ago Up 39 seconds 3306\/tcp mariadbdemo<\/code><\/pre>\n\n\n\n
docker ps -a<\/strong><\/code> command.<\/p>\n\n\n\n
docker ps -q<\/code><\/pre>\n\n\n\n
Checking MariaDB Container Logs<\/h4>\n\n\n\n
docker logs <name-or-container-id><\/code><\/em> command;<\/p>\n\n\n\n
docker logs mariadbdemo<\/code><\/pre>\n\n\n\n
docker logs ce03099cfffa<\/code><\/pre>\n\n\n\n
Accessing MariaDB Docker Container<\/h3>\n\n\n\n
\n
docker exec -it <name-of the container|container-id> <shell><\/strong><\/code>.<\/p>\n\n\n\n
docker exec -it mariadbdemo bash<\/code><\/pre>\n\n\n\n
root@9f26c99c35a5:\/#<\/code><\/pre>\n\n\n\n
apt install mysql-client<\/code><\/pre>\n\n\n\n
docker inspect <name-of-the-container|container-id><\/strong><\/code> command which provides a complete details about the container.<\/p>\n\n\n\n
docker inspect mariadbdemo | grep IPAddress<\/code><\/pre>\n\n\n\n
\"SecondaryIPAddresses\": null,\n \"IPAddress\": \"172.17.0.2<\/strong>\",\n \"IPAddress\": \"172.17.0.2\",<\/code><\/pre>\n\n\n\n
mysql -u root -p -h 172.17.0.2<\/code><\/pre>\n\n\n\n
MYSQL_ROOT_PASSWORD<\/code><\/strong> variable when running the container above.<\/p>\n\n\n\n
Enter password: password<\/strong>\nWelcome to the MySQL monitor. Commands end with ; or \\g.\nYour MySQL connection id is 5\nServer version: 5.5.5-10.5.4-MariaDB-1:10.5.4+maria~focal mariadb.org binary distribution\n\nCopyright (c) 2000, 2020, Oracle and\/or its affiliates. All rights reserved.\n\nOracle is a registered trademark of Oracle Corporation and\/or its\naffiliates. Other names may be trademarks of their respective\nowners.\n\nType 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.\n\nmysql><\/code><\/pre>\n\n\n\n
Accessing MariaDB Docker Container Externally<\/a><\/h4>\n\n\n\n
ss -altnp | grep 3306<\/code><\/pre>\n\n\n\n
docker run <\/code>options,
-p <host-port>:<container-port><\/strong><\/code>. See example command below to expose MariaDB container port 3306 via the host using the same port;<\/p>\n\n\n\n
docker run --name mariadbdemoport -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mariadb<\/code><\/pre>\n\n\n\n
docker ps<\/code><\/pre>\n\n\n\n
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\n3489ce131cce mariadb \"docker-entrypoint.s\u2026\" 4 minutes ago Up 2 seconds 0.0.0.0:3306->3306\/tcp mariadbdemoport\nce03099cfffa mariadb \"docker-entrypoint.s\u2026\" About an hour ago Up 40 seconds 3306\/tcp mariadbdemo<\/code><\/pre>\n\n\n\n
-p host-IP-address:Port:<container-port><\/code><\/strong>.<\/p>\n\n\n\n
ss -altnp | grep 3306<\/code><\/pre>\n\n\n\n
LISTEN 0 4096 *:3306 *:*<\/code><\/pre>\n\n\n\n
-p <container-port><\/code><\/pre>\n\n\n\n
docker run --name mariadbdemo-dport -p 3306 -e MYSQL_ROOT_PASSWORD=password -d mariadb<\/code><\/pre>\n\n\n\n
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES\nbee9f036aedd mariadb \"docker-entrypoint.s\u2026\" 8 seconds ago Up 3 seconds 0.0.0.0:32768->3306\/tcp mariadbdemo-dport<\/strong>\n3489ce131cce mariadb \"docker-entrypoint.s\u2026\" 12 minutes ago Up 7 minutes 0.0.0.0:3306->3306\/tcp mariadbdemoport\nce03099cfffa mariadb \"docker-entrypoint.s\u2026\" About an hour ago Up 8 minutes 3306\/tcp mariadbdemo<\/code><\/pre>\n\n\n\n
docker port<\/strong><\/code> command;<\/p>\n\n\n\n
docker port mariadbdemo-dport<\/code><\/pre>\n\n\n\n
3306\/tcp -> 0.0.0.0:32768<\/strong><\/code><\/pre>\n\n\n\n
Setting Persisting Data Volume for MariaDB Docker Container<\/h3>\n\n\n\n
-v <host-directory>:<container-directory><\/strong><\/code> option of
docker run<\/code><\/strong> command.<\/p>\n\n\n\n
\/var\/lib\/mysql<\/strong><\/code>.<\/p>\n\n\n\n
\/opt\/mariadb\/data<\/strong><\/code>, the first create the directory and run a container mapping the data directories using the
-v<\/strong><\/code> option.<\/p>\n\n\n\n
mkdir -p \/opt\/mariadb\/data<\/code><\/pre>\n\n\n\n
docker run --name mariadbdemo-volume -v \/opt\/mariadb\/data:\/var\/lib\/mysql -e MYSQL_ROOT_PASSWORD=password -d mariadb<\/code><\/pre>\n\n\n\n
\/var\/lib\/mysql<\/strong><\/code>, will be written to the data directory on host,
\/opt\/mariadb\/data<\/code> at the same time.<\/p>\n\n\n\n
Stop, Pause, Restart Remove Docker Container<\/h3>\n\n\n\n
docker stop<\/strong><\/code> command;<\/p>\n\n\n\n
docker stop [option] <name|id><\/code><\/pre>\n\n\n\n
docker stop mariadbdemo<\/code><\/pre>\n\n\n\n
docker stop $(docker ps -q)<\/code><\/pre>\n\n\n\n
docker rm [option] <name|id><\/code><\/pre>\n\n\n\n
docker rm mariadbdemo<\/code><\/pre>\n\n\n\n
docker rm --force mariadbdemo<\/code><\/pre>\n\n\n\n
docker pause mariadbdemo<\/code><\/pre>\n\n\n\n
docker restart mariadbdemo<\/code><\/pre>\n\n\n\n
docker volume ls<\/code><\/pre>\n\n\n\n
docker inspect<\/code> command.<\/p>\n\n\n\n
docker volume rm volume-name<\/code><\/pre>\n\n\n\n
Reference<\/h3>\n\n\n\n
Other Related Tutorials<\/h3>\n\n\n\n