MariaDB, a fork of open source MySQL can be easily installed on a Docker container to create Database for various web applications such as WordPress, OwnCoud, etc. The benefit of using MariaDB on Docker container is you will not need a dedicated server to separate your Database server from the rest of the applications.
However, learning curves will be there to manage containers. And here we will help you understand the basic steps to install the MariaDB container and how to run and access it remotely to manage databases.
Steps to install MariaDB container on Docker CE
Setup Docker Engine
The first thing that must be on your Windows or Linux server is the Docker. Although if you are here then you would already have it, if not then see the below-given links:
- How to install Docker CE on Rocky Linux/AlmaLinux/CentOS 8
- Tutorial to install Docker-container on Ubuntu 20.04 LTS…
- Steps to install Docker on Windows 10
Install MariaDB Docker Image
To set up containers, we need the Image of the particular application that we want to install in that. You can create a Docker app image by yourself, however, luckily the developers of Docker also provide a repository of images called Docker Hub. From where we can easily pull the latest images of hundreds of applications to instantly create and start with containers. And MariaDB is also present in the Hub. Here is the Hub Page featuring MariaDB.Â
To install the latest version of MariaDB on Docker simply run the pull command-
docker pull mariaDB
Note: In case you want to install some previous version, then see the link given for its Hub page above and use the available tags.
Run MariaDB Container and also forward 3306 port
Now, there are multiple things we can perform while starting the installed MariaDB container. Here are different scenarios and commands corresponding to them.
Scenario 1: Set MariaDB docker container root password
You want to run MariaDB that can be accessed only by the host and other containers. Then, run:
docker run --name mariadbh2s -e MYSQL_ROOT_PASSWORD=password -d mariadb
Note: Change the bold items in the above command as per your choice.
Flags used in the above command:
--name mariadbh2s
– To set the name of the container otherwise it will get assigned one randomly.
-e MYSQL_ROOT_PASSWORD=password
– For setting root password to Mariadb.
-d
is to run the container in the background.
Scenario 2: Forward Container port to host
You want to run the MariaDB container and also want to access the Database server from remote computers. Then map container’s MySQL port 3306 to host port 3306 port.
For this we have to add a syntax or flag:Â -p
host-port:conainer-port
For example- Here it will be like this: -p 3306:3306
 and just add it in the command to forward the port.
docker run --name mariadbh2s -p 3306:3306Â -e MYSQL_ROOT_PASSWORD=password -d mariadb
Trivia– We can also assign some host ports dynamically to the container, in case the default port of MariaDB is occupied on the host.
For that, the syntax will be like this: -p <container-port> or -p 3306.
However, when you assign a dynamic port, to find out which port has been assigned and run-
docker ps
In the result, you will get the active container details including the ports that have been mapped with the host.
Scenario 3: MariaDB Docker Container volume mapping
As we know Database is an important thing for any application and in such a scenario if your container gets deleted, all the data will be gone. Thus, it will a good idea if we create a mirror copy of Docker MariaDB Volume on the host filesystem. As you add anything to the MariaDB Docker container database server’s internal volume this will also get copied to the mapped host directory. In this way, we always have a backup copy of all our container data.
Let’s create a directory, say /opt/mariadb/backup
on the host to map it with the docker container of mariadb.
On the host system terminal type-
sudo mkdir -p /opt/mariadb/backup
-p
stands for parent directory – means it will hold other files.
Now, coming to the main command. The default volume or directory where all the data of mariadb container stores is – /var/lib/mysql
on docker. Thus, to map it with the one we have created, use the following command syntax with the -v flag.
-v hostdirectory:container-direcotry
Hence, if you go with the one we have created then the above syntax will be like this –
-v /opt/mariadb/backup:/var/lib/mysql
And in the docker command-
docker run --name mariadbh2s -p 3306:3306 -v /opt/mariadb/backup:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mariadb
So, in the above commands, we have not only learned how to forward the port but also have mapped the volume that will remain on the host even after destroying the container.
Check Install MariaDB version
If you want to check the installed MariaDB container version without switching to its bash. Then here is the syntax-
docker exec -it container-name mysql -V
Note: Replace the container-name with the one you have set.
Access MariaDB Docker Container bash
Once the container is running you can access its command bash directly from your host terminal using this command-
docker exec -it contain-name /bin/bash
Here, in our case the container name is mariadbh2s, thus the above command will be like this-
docker exec -it mariadbh2s /bin/bash
Note: Use your assigned container name in the above command.
To quit, type-
exit
Log in to Docker running container’s Database server from the host terminal
Although once you are in container’s bash using the above command, you can access the database server by simply typing mysql -p
However, if you don’t want to switch every time to its container root shell, then it is possible to directly access MariaDB from the host terminal. For that install MySQL client on Host.
For example, if your host is on Debian or Ubuntu-
sudo apt install mysql-client
Whereas, RHEL based systems can use-
sudo yum install mysql-client
Now, find out the IP address of Mariabd running container-
docker inspect container-name | grep IPAddress
Note– Replace the Container-name with the one you have assigned.
Connect to the Ip-address you have got to log in to your MariaDB container.
mysql -u root -p -h ip-address
Example-
mysql -u root -p -h 172.17.0.2
Other Docker Container commands
Stop
docker stop container-name
Pause
docker pause container-name
Restart
docker restart container-name
Remove
docker rm container-name
If you get some error then use force flag-
docker rm --force container-name
Well, if you are using Kitematic or any other Focker GUI then the above command can be easier to handle. –
See-
- Best Docker GUI to install and manage containers
- How to install Kitematic Docker GUI on Ubuntu 20.04 LTS…
- How to install Kitematic docker manager on CentOS
you;re not installing man , you;re just pulling an image which already has mariadb db installed
How do i install not pull the image mysql insided a container