Bootstrap FreeKB - mySQL / MariaDB - Install MariaDB on Docker
mySQL / MariaDB - Install MariaDB on Docker

Updated:   |  mySQL / MariaDB articles

A Docker image contains the code used to create a Docker container, such as creating a Nginx web server, or a mySQL server, or a home grown app, and the list goes on. In this way, an image is like a template used to create a container. An image is kind of like a virtual machine, but much more light weight, using significantly less storage a memory (containers are usually megabytes in size).

 

The docker pull command can be used to pull down the latest MariaDB image.

~]# docker pull mariadb
Using default tag: latest
latest: Pulling from library/mariadb
7b1a6ab2e44d: Pull complete
034655750c88: Pull complete
f0b757a2a0f0: Pull complete
5c37daf8b6b5: Pull complete
b4cd9409b0f6: Pull complete
dbcda06785eb: Pull complete
a34cd90f184c: Pull complete
fd6cef4ce489: Pull complete
3cb89a1550ea: Pull complete
df9f153bd930: Pull complete
Digest: sha256:c014ba1efc5dbd711d0520c7762d57807f35549de3414eb31e942a420c8a2ed2
Status: Downloaded newer image for mariadb:latest
docker.io/library/mariadb:latest

 

The docker images command can be used to display the MariaDB image.

~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mariadb      latest    12e05d5da3c5   13 days ago   409MB

 

The following command can then be used to create and start the MariaDB container. Let's break down this command.

  • The docker run command is used to create and start the container.
  • The --detach flag is used to run the container in the background.
  • The --publish option is used to configure both the Docker server and container to listen on port 3306, which adds a rule to iptables to allow connections between the Docker system and container on port 3306.
  • The --env option is used to define the MARIADB_ROOT_PASSWORD variable.
  • The --name option is used to name the container mariadb.
  • The --restart unless-stopped option is used so that the container is started if the Docker server is restarted
  • The mariadb image is used.
docker run
--detach
--publish 3306:3306
--env MARIADB_ROOT_PASSWORD=itsasecret
--name mariadb
--restart unless-stopped
mariadb

 

Optionally, you could also have the .my.cnf file somewhere on your Docker system, such as at /usr/local/docker/mariadb/root/.my.cnf, where the .my.cnf file contains root password, so that root can connect to MariaDB without having to provide password. The .my.cnf file would contain something like this.

[client]
password=itsasecret

 

You would then use the --volume option to mount /usr/local/docker/mariadb/root/.my.cnf on your Docker system to /root in the container.

docker run
--detach
--publish 3306:3306
--volume /usr/local/docker/mariadb/root:/root
--env MARIADB_ROOT_PASSWORD=itsasecret
--name mariadb
--restart unless-stopped
mariadb

 

You will most likely also want to configure MariaDB so that a remote connection can be made to MariaDB. To be able to make a remote connection to the container, the /etc/mysql/mariadb.conf.d/50-server.conf file in the container will need to have the bind address of the container. For this reason, you may want to use the docker cp command to copy the files in the /etc/mysql/mariadb.conf.d directory in the container to a directory on your Docker system, such as /usr/local/docker/mariadb/mariadb.conf.d.

docker cp mariadb:/etc/mysql/mariadb.conf.d /usr/local/docker/mariadb

 

Then edit the 50-server.conf file to have the IP address of the container.

bind-address=172.16.0.2

 

You would then use the --volume option to mount /usr/local/docker/mariadb/mariadb.conf.d directory on your Docker system to /etc/mysql/mariadb.conf.d in the container, and also use the --ip option to assign an IP address to the container.

docker run
--detach
--publish 3306:3306
--volume /usr/local/docker/mariadb/root:/root
--volume /usr/local/docker/mariadb/mariadb.conf.d:/etc/mysql/mariadb.conf.d
--ip 172.16.0.2
--env MARIADB_ROOT_PASSWORD=itsasecret
--name mariadb
--restart unless-stopped
mariadb

 

The docker container ls command can be used to ensure the container is running.

~]# docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                                       NAMES
c132e43cd93a   mariadb   "docker-entrypoint.s."   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp   mariadb

 

The docker logs command should return something like this.

2021-10-30  1:19:40 0 [Note] mysqld: ready for connections.
Version: '10.6.4-MariaDB-1:10.6.4+maria~focal'  socket: '/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

 

The docker exec command with the -it or --interactive --tty files can be used to get a shell on the container.

docker exec -it mariadb bash

 

You should be able to connect to MariaDB as root, using the password that you defined in the MARIADB_ROOT_PASSWORD variable.

root@c132e43cd93a:/# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.6.4-MariaDB-1:10.6.4+maria~focal mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 037ede in the box below so that we can be sure you are a human.