Installing and Using MariaDB via Docker
220202 – 04 & 220519
Intro
In one of my previous posts here, I’ve written about how to install MariaDB in your macOS via the Homebrew package manager. Here, I present shortly, how you can quickly install it via Docker. So, it is presumed that you already have Docker installed in your system and you are familiar with it.
Installation
You can go to the Docker hub here and choose the image with the tag for your preferable version of MariaDB. Here, we will use the latest image which includes MariaDB version 10.7.3.
Open a terminal window and run the following (long) command:
docker run –detach –name mariadb-gen -p 3306:3306 –env MARIADB_USER=user1 –env MARIADB_PASSWORD=pwuser1 –env MARIADB_ROOT_PASSWORD=pwroot mariadb:latest
mariadb:latest
The Docker image name of MariaDB with the tag ‘latest’ that we will be fetched from Docker Hub and we will use it to create our container.
mariadb-gen
The name of the container we are going to create
-p 3306:3306
The container’s port MariaDB is listening to (the “2nd” 3306), is exposed as the same port the “1st” 3306) of the host (macOS here). Note that if a locally installed MariaDB is running in the same port, it should have been stopped, before the installation, otherwise you have to change the host’s port for that container being created, e.g to 3307::
docker run –detach –name mariadb-gen -p 3307:3306 –env MARIADB_USER=user1 –env MARIADB_PASSWORD=pwuser1 –env MARIADB_ROOT_PASSWORD=pwroot mariadb:latest
MARIADB_USER
The environment variable to pass into the container as a user name
MARIADB_PASSWORD
The environment variable to pass into the container for creating the password for the user with the name previously passed in by the environment variable MARIADB_USER.
MARIADB_ROOT_PASSWORD
The environment variable to pass into the container as the password for the administrator (root) name
After a while, the installation should have been completed and a new container with the name ‘mariadb-gen‘. Then, you can use the standard docker commands to work with, e.g.:
Checking running and existing (all running and stopped) containers
docker ps
docker ps -a
Starting and stopping the container
docker start mariadb-gen
docker stop mariadb-gen
Running the container shell
docker exec -it mariadb-gen /bin/bash
Getting the network the container runs
docker inspect -f ‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ mariadb-gen
(It returns the <network IP> of the container, eg.: 172.17.0.2)
Running the mysql CLI within the container shell
docker run -it –rm mariadb:latest mysql -h <network IP> -u root -p
e.g: docker run -it –rm mariadb:latest mysql -h 172.17.0.2 -u root -p
That’s it!
Enjoy, and thank you for reading!