MariaDB is an Open Source fork from one of the most used relational databases, MySQL, and for developers, having our dev database locally runing can be pretty useful. Docker can solve this. In this post we will cover two ways to create and configure MariaDB in a container: Docker CLI and Portainer CE.
Requisites
To properly continue with this post, you must meet the following requisites:
- Have docker installed, you can follow this guide I’ve done.
- Have some DB Admin GUI/CLI, I use DBeaver.
- Portainer CE installed (just for the second part).
Using Docker CLI
This one is the most traight forward and more popular. With a single command, we will have the service running, with a default user and database, just change the password enviroment variable:
docker run --name mariadb -e MARIADB_ROOT_PASSWORD=<rootpassword> -p 3306:3306 -d mariadb
The command above launched everything we need to start. But we can configure way more things before launching, like the username, or the default database name. But first, lets remove the container created before:
docker container rm -f mariadb
Now, we are ready to execute the new container. Remember to change the enviroment variables values with your needs:
docker run --name mariadb -e MARIADB_USER=<user> -e MARIADB_PASSWORD=<password> -e MARIADB_DATABASE=<database> -e MARIADB_ROOT_PASSWORD=<rootpassword> -p 3306:3306 -d mariadb
Now that we have MariaDB image downloaded, the output should be as simple as a one line. But is more, other of the important things to do, at least with databases, is to map the path where the data is stored to a Docker volume, so if we update the container, we don’t lose the data stored. For MariaDB, the path inside the container is /var/lib/mysql
, so let’s map it. Don’t forget to remove the container created before as we did earlier!
docker run --name mariadb -e MARIADB_USER=<user> -e MARIADB_PASSWORD=<password> -e MARIADB_DATABASE=<database> -e MARIADB_ROOT_PASSWORD=<rootpassword> -p 3306:3306 --volume mariadb_data:/var/lib/mysql -d mariadb
With this last command, we have MariaDB up and running locally, with the data stored on a Docker volume so we don’t lose it between updates.
Docker CLI and Compose
But what about a docker-compose file? Let’s create the .yaml to do the same as the command before:
version: '3.5'
services:
mariadb:
image: mariadb
restart: always
environment:
MARIADB_USER: <user>
MARIADB_PASSWORD: <password>
MARIADB_DATABASE: <database>
MARIADB_ROOT_PASSWORD: <rootpassword>
volumes:
- mariadb_data:/var/lib/mysql
ports:
- 3306:3306
volumes:
mariadb_data:
Before executing this command, check the docker-compose version tag compatibility with your docker version on this list, change it if needed, and just type this command, substituting the path and name with your needs:
docker compose -f <path_to_file>/<file_name>.yml up -d
And it’s done!
Using Portainer GUI
Other way I use to deploy my containers and stacks, is Portainer CE, which provides a pretty useful web GUI to do all the Docker tasks I need, and it also support Docker Swarms. To achieve the same task of deploying a MariaDB container, we can take two ways: Container deployment, or Docker Compose / Stacks. Let’s start with the first one, but before let’s ensure that there is no container with MariaDB already running, and if there is one, remove it:
docker container rm -f mariadb
Now you can go to the web GUI, select your node and go to “Volumes” on the left menu:
![Screenshot from Portainer's web interface, where the volume list is visible.](/wp-content/uploads/2023/10/image-38-1024x261.png)
Once you are on the volume list (you may have no one created yet), click on “+ Add volume” at the top right:
![Screenshot from Portainer's web interface, where the new volume form is visible.](/wp-content/uploads/2023/10/image-39-1024x557.png)
Give a name to this volume, and leave the other options. Then, click on “Create the volume“, and go to “Containers” on the left menu.
![Screenshot from Portainer's web interface, where the container list is visible.](/wp-content/uploads/2023/10/image-40-1024x271.png)
Click on “+ Add container“, give it a name and pick the MariaDB image:
![Screenshot from Portainer's web interface, where the new container form is visible, with the basic configuration.](/wp-content/uploads/2023/10/image-41-1024x192.png)
Down below, let’s add the port configuration, by clicking on “+ publish a new network port” and using 3306
on both host and container.
![Screenshot from Portainer's web interface, where the new container form is visible, with the port configuration.](/wp-content/uploads/2023/10/image-42-1024x169.png)
At the bottom, we have the Advanced container settings where we can select our volume and add our enviroment variables. Let’s start with the volume: Click on “+ map additional volume” and fill container’s field with the path /var/lib/mysql
; for the volume, select the one we have created before.
![Screenshot from Portainer's web interface, where the new container form is visible, with the volume configuration.](/wp-content/uploads/2023/10/image-43-1024x286.png)
Now, let’s click on “Env” to set up the enviroment variables, and click “+ Add an enviroment variable” for each one of the following, and fill the values:
- MARIADB_USER
- MARIADB_PASSWORD
- MARIADB_DATABASE
- MARIADB_ROOT_PASSWORD
![Screenshot from Portainer's web interface, where the new container form is visible, with the enviroment variables.](/wp-content/uploads/2023/10/image-44-1024x485.png)
Now click on “Deploy container” on the top left, just above of “Advanced container settings“:
![Screenshot from Portainer's web interface, where the new container form is visible, with the Deploy Container button displayed.](/wp-content/uploads/2023/10/image-45-1024x214.png)
And it’s done! You have your container created:
![Screenshot from Portainer's web interface, where the container list is visible with the new container created.](/wp-content/uploads/2023/10/image-46-1024x271.png)
But now… What about that easy way with “docker-compose” we had with Docker CLI?
Portainer GUI and Stacks (Compose)
If you created that container before, either use other port on the host or remove the container. Then, go to “Stacks” on the left menú:
![Screenshot from Portainer's web interface, where the stack list is visible.](/wp-content/uploads/2023/10/image-48-1024x215.png)
Click on “+ Add stack“, give it a name:
![Screenshot from Portainer's web interface, where the new stack form is visible, with the basic configuration.](/wp-content/uploads/2023/10/image-49-1024x238.png)
And here, select either “Upload” and pick that docker-compose.yml
that we created before; or select “Web editor” to paste this:
version: '3.5'
services:
mariadb:
image: mariadb
restart: always
environment:
MARIADB_USER: <user>
MARIADB_PASSWORD: <password>
MARIADB_DATABASE: <database>
MARIADB_ROOT_PASSWORD: <rootpassword>
volumes:
- mariadb_data:/var/lib/mysql
ports:
- 3306:3306
volumes:
mariadb_data:
Should look like this:
![Screenshot from Portainer's web interface, where the new stack form is visible, with the stack web editor.](/wp-content/uploads/2023/10/image-51-1024x450.png)
Remember to add the enviroment variables down below with your user, password and database, like we did before. Now just click on “Deploy the stack“, and the stack will deploy!
Check connection
Open your DB Admin GUI of your preference. I use DBeaver as mentioned in the requisites section. Once opened, click on “New connection” on the top left:
![Screenshot of DBeaver's interface, on the new connection window, with MariaDB selected.](/wp-content/uploads/2023/10/image-54-1024x766.png)
Select MariaDB and click on “Next >” to go to MariaDB connection configuration.
![Screenshot of DBeaver's interface, on the new connection window, with MariaDB configuration form.](/wp-content/uploads/2023/10/image-55-1024x759.png)
Fil it with your connection user, password and database, and now click on “Test Connection …“.
![Screenshot of DBeaver's interface, with a popup message of the connection test.](/wp-content/uploads/2023/10/image-56.png)
Now we checked that the database is created an listening to connections! Click on finish and then you will be able to play around with your new DB instance of MariaDB on Docker.
Conclusions
With this guide, we have learned:
- How to create a Docker container with Docker’s CLI, in this case, a MariaDB database, but it’s mostly applicable to every container, just chainging variables, volumes, or other params.
- About Portainer, it’s GUI and how to create containers and stacks there.
- That compose/stacks are a quite easy way to deploy a maintain containers.
- How to create and test a connection using DBeaver as a DB GUI.
Hope it was simple and helpful! Feel free to ask any questions or commenting in this post!