opened image

How to install MariaDB in Docker

In this article, we will look at how to install the MariaDB MySQL server in a Docker container and see how to connect it to phpMyAdmin in the docker-compose that we installed in the article How to install phpMyAdmin in Docker.
 

So, let's install Docker. 

As usual, we update the OS packages. 
 

apt update


We will install the necessary packages and add a new repository:

 

 

 

apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

 


We update the packages with the new repository:

 

 

 

 

apt update

 


We install Docker.

 

 

 

 

apt-get install docker-ce docker-ce-cli containerd.io

 


We check the Docker version:

 

 

 

 

docker --version

 


We check the status:

 

 

 

 

systemctl status docker

 



If it did not start, we start it:

 

 

 

 

systemctl start docker

 


And add it to autostart.

 

 

 

 

systemctl enable docker

 


 

 

Installing Docker-Compose

 

 
curl -L "https://github.com/docker/compose/releases/download/1.25.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose


We set execution permissions for the docker-compose file. 

 

 

 

chmod +x /usr/local/bin/docker-compose

 


We check if Docker-Compose is installed:

 

 

 

 

docker-compose --version

 



As we can see, everything is fine. Let's create a file for Docker-Compose.

To orient ourselves in the future about where and what we have installed, we will create a separate folder for this project in the /home directory and navigate to it.

 

 

 

 

mkdir /home/mariadb && cd /home/mariadb

 


In this guide, we will install a specific version of MariaDB 10.8. But you can install the one you need from those available on the hub.docker.com website.

To create the docker-compose.yaml file, we will use the repository at the link https://hub.docker.com/_/mariadb

We create the docker-compose.yaml or docker-compose.yml file:

 

 

 

 

vim docker-compose.yaml

 


And add the following code to it:

 

 

 

version: '3.1'

services:
  mariadb:
    container_name: mariadb
    image: mariadb:10.8
    ports:
        - 3310:3306
    environment:
        MARIADB_USER: admin
        MARIADB_PASSWORD: your_password
        MARIADB_ROOT_PASSWORD: your_strong_pass
    restart: always



Where:

mariadb: the name of your container;
image: mariadb:10.8: the image from which mariadb will be deployed;
ports: 3310:3306 - port 3310 that we will use to connect to mariadb;
restart:always - indicates that the container will be restarted in case of failure or server reboot;
MARIADB_USER: creating a new user, in this case, it is admin;
MARIADB_PASSWORD: the password for the admin user;
MARIADB_ROOT_PASSWORD: this password will be set for the root superuser account of MariaDB.

We run our script (for this you need to be in the directory where our file was created. In this case, it is /home/mariadb):

 

 

 

 

 

docker-compose up -d 

 


We wait for the images to download and deploy.

We check:

 

 

 

 

docker-compose ps

 


or 

 

 

 

 

docker ps

 



To view the logs, use the command 

 

 

docker logs -f mariadb



You can also perform the installation directly in docker:

 

 

 

docker run -d --name mariadb -e MARIADB_USER=admin -e MARIADB_PASSWORD=your_password -e MARIADB_ROOT_PASSWORD=your_strong_pass mariadb:10.8

 



You can also install this version of the database mariadb:10.8 along with phpMyAdmin and link it to it. Let's see how to do this.

We will edit the created docker-compose.yaml file.

 

 

 

 

vim docker-compose.yaml

 



We will add the following structure below:

 

 

 

version: '3.1'

services:
  mariadb:
    container_name: mariadb
    image: mariadb:10.8
    ports:
        - 3310:3306
    environment:
        MARIADB_USER: admin
        MARIADB_PASSWORD: your_password
        MARIADB_ROOT_PASSWORD: your_strong_pass
    restart: always

  phpmyadmin:
    image: phpmyadmin
    restart: always
    ports:
      - 8090:80
    environment:
      - PMA_HOST=mariadb
    depends_on:
      - mariadb

 


In the phpmyadmin  section, we added the phpmyadmin image, specified port 8090, but now it will be linked to a specific database server, namely the one we installed/deployed mariadb:10.8.
This parameter is specified in the environment  block where: PMA_HOST=mariadb - points to the mariadb  container/block described above in this file.
The depends_on: directive indicates the dependency of the start/run on the mariadb container. This means that until the container with mariadb starts, the container with phpmyadmin  will not be started. We can start phpmyadmin  and mariadb with one command. 

 

 

 

 

 

docker-compose up -d or docker-compose up -d --force-recreate


We check:

 

 

 

 

 

docker-compose ps

 



In this way, you can install the version of the MariaDB or MySQL database that you need. 

How to install Postgres and pgadmin4 in Docker can be read in the article How to install PostgreSQL and pgAdmin in Docker.