SIGN IN / UP
    opened image

    In this article, we will look at how to install a 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
    


    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"
    

     


    Update packages with the new repository:

     

     

     

     

    apt update
    

     


    Install Docker.

     

     

     

     

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

     


    Checking Docker version:

     

     

     

     

    docker --version
    

     



    Let's see the status:

     

     

     

     

    systemctl status docker
    

     



    If it doesn't start, run:

     

     

     

     

    systemctl start docker
    

     


    And add to autorun.

     

     

     

     

    systemctl enable docker
    

     


     

     
    Install 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
    
    Set run permissions for docker-compose.
    
    chmod +x /usr/local/bin/docker-compose



    Check if Docker-Compose is installed:

     

     

     

    docker-compose --version
    

     




    As you can see, everything is in order. Let's move on to creating a file for Docker-Compose.

    To navigate in the future where and what we have installed, let's create a separate folder for this project in the /home directory and go to it.

     

     

     

     

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

     


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

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

    Create a 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 which we will use to connect to mariadb
    restart:always - indicates that the container will be restarted when the server crashes or restarts
    MARIADB_USER: creating a new user in this case is admin;
    MARIADB_PASSWORD: password for the admin user;
    MARIADB_ROOT_PASSWORD: This password will be set for the root account of the MariaDB superuser.

    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 are waiting for the download of images, and deployment.

    We check:

     

     

     

     

    docker-compose ps
    

     


    or

     

     

     

     

    docker ps
    

     




    To view the logs, use the command:

     

     

     

     

    docker logs -f mariadb

     



    You can also install only 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
    

     



    Also, this version of the mariadb:10.8 database can be installed along with phpMyAdmin and linked to it. Let's see how to do it.

    Let's edit the created docker-compose.yaml file

     

     

     

     

    vim docker-compose.yaml
    

     



    Add the following structure below the created one:

     

     

     

     

    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 sector, we added the phpmyadmin image, specified port 8090 to it, but now it will be bound to a specific database server, namely the one that we installed/deployed mariadb:10.8
    This parameter is written in the enviroment block where:
    PMA_HOST=mariadb - points to the mariadb container/block which is described above in this file.
    Also, the depends_on: directive indicates the dependency of the start/run on the mariadb container. This means that until the container with mariadb is started, the container with phpmyadmin will not be started.

    We can start phpmyadmin and mariadb with one command.

     

     

     

     

    docker-compose up -d
    

     


    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 found in the article How to install PostgreSQL and pgAdmin in Docker