Table of Contents
- Step 1 - Pull docker image: This command will pull the MariaDB docker image from the docker hub
- Step 2 - Setup the environment: You can store all the environment variables in Docker Secrets using Swarm Service
- Step 3 - Run docker image
- Step 4 - Setup MariaDB
In this article, I will walk you through the process of installing MariaDB on a new Docker container. This process is useful if you’re using Docker containers to host different applications and orchestrate them all together in a highly available way.
Docker is an open platform for developing, shipping and running applications in a distributed environment. It provides developers with tools to create lightweight, portable and reusable containers from any application.
MariaDB is an open-source database server. Originally developed as a MySQL replacement, it is proven to be fast, reliable, and scalable. In addition, MariaDB has maintained compatibility with MySQL features and handles data storage elegantly. It’s used by thousands of organizations around the world because of its high availability and security features.
Now, we have a better idea about docker and MariaDB. Let's get into the real deal. To continue this part of the blog you have to install docker on your computer or server first. Once you've done that, set up MariaDB on that same environment for further installation.
docker pull mariadb
Step 2 - Setup the environment: You can store all the environment variables in Docker Secrets using Swarm Service
In Docker secrets, you use a secret name to store credentials and values that are shared across different Docker hosts. This includes all types of secrets, such as database access credentials or API keys that are used by your application. When you set a secret in the registry, it will be stored as a host-level secret.
docker swarm init
Root password: Store the root password as a secret to pass when running the image.
printf "your_root_password" | docker secret create root_password -
In this article, I'll explain how to create a new overlay network for docker swarm and MariaDB. As I'm using swarm service and running MariaDB as a service, I decided to use an overlay network adapter. To learn more about overlay docker network adapter refer to Docker network basics
docker network create -d overlay mariadb_private
I run the MariaDB image as a docker service to use with the swarm service. If you are not using swarm service you can run the image with the docker run command as below
docker run -d --name mariadb \ -p 3306:3306 \ -e MYSQL_ROOT_PASSWORD=root_password \ -n mariadb_private \ mariadb
But, here we use the service method.
docker service create \ --name mariadb \ --restart-condition any \ --network mysql_private \ --publish published=3306,target=3306 \ --mount type=volume,source=mydata,destination=/var/lib/mysql \ --secret source=root_password,target=root_password \ -e MARIADB_ROOT_PASSWORD_FILE="/run/secrets/root_password" \ mariadb:latest
These are the values I passed in the above command:
- --name: Name of the service
- --restart: Restart when the condition is met ("none"|"on-failure"|"any") (default "any")
- --network: Network attachment. Here, we can use the network created in the previous step.
- --publish: Publish a port as a node port
- --mount: Mount a file-system
- --secret: Store or retrieve an arbitrary secret in the key/value store in docker secrets. (default " none")
- -e --env: Environment variables. Here I pass the root password which I stored in secrets.
So first of all, let's log in to the MariaDB container so that we can set up the database. If you haven't passed the root password in environment variables you can set it by running
docker exec -it container /bin/bash
iii. If you have passed the MariaDB root password in environment variables, you can immediately log in to the MySQL command line with the following command
docker exec -it container mysql -uroot -p
iv. Once you log in to the MySQL command line using your credentials, you can create your new database and start setting up your MySQL configuration. Be aware that this creates the database structure, user accounts, and grants.
- Create Database :
CREATE DATABASE YOUR_BD;
Use the following command to check whether your database has been created or not.
- Create user :
CREATE USER 'your_user'@'%' IDENTIFIED BY 'your_password';
use the following command to check your created user,
SELECT user FROM mysql.user;
- To grant permissions to a user for a particular database, you can use the GRANT command. Most likely, you have already granted the user default permissions for tables in that particular database. By using the GRANT command, you can add more rights to the existing users, give permissions to new users and change or delete user privileges.
GRANT ALL PRIVILEGES ON YOUR_DB.* TO 'your_user'@'%' IDENTIFIED BY 'your_password';` and then run, `FLUSH PRIVILEGES;
Once you have executed the MySQL commands above, you can check the access with the below command,
SHOW GRANTS FOR 'your_user'@'%';
In this article, MariaDB installation is discussed. It helps in ensuring that MariaDB can be reliably used together with other applications in the Docker environment. Also, the Docker overlay network description is discussed here. Please let me know your thought and ideas in the comment section. Also, I would like to know if you have any questions regarding Docker and MariaDB, I'm happy to help you.