Skip to content

Mariadb

Iva edited this page Aug 23, 2024 · 11 revisions

Mariadb Docker

  • Initial Configuration for MariaDB

    • Create the Docker image configuration file Dockerfile for MariaDB in the path described below via the terminal:

      mkdir -p rootInception/srcs/requirements/mariadb/Dockerfile
    • Update the docker-compose.yml file by adding a service for MariaDB so that when executed, it can compose the MariaDB container as well, as shown below:

      version: "3.8"
      
      services:
        nginx:
          build: requirements/nginx/.
          container_name: nginx
          ports:
            - "80:80"
          volumes:
            - .web:/var/www/html
          
        wordpress:
          build: requirements/wordpress/.
          container_name: wordpress
          volumes:
            - .web:/var/www/html
          
        mariadb:
          build: requirements/mariadb/.
          container_name: mariadb
    • Fill in the Dockerfile for MariaDB so that it can configure a Docker image that will be executed later as shown below:

      FROM debian:bullseye 
      RUN apt update && apt upgrade -y && apt install -y mariadb-server
      ENTRYPOINT ["mysqld_safe"]

      👉🏼 These are the minimum configurations for MariaDB to run inside the container. We now need to start the MariaDB container, enter it, and copy the content of some default files created during the MariaDB installation in the system, which should be modified for the project's functionality.

    Testing initial config to Mariadb

    • Execute the command that will create and start the containers, as described below, via the terminal:

      docker compose up --build

      👉🏼 For the test to work, clear all created Docker images and recreate them from the updated Dockerfile and docker-compose file configurations.

  • Configuration File Copy from Host(Automatic Mode)

    • Execute the copy command via the container manager compose, to copy the default file from the MariaDB container to the host, as described below:

      docker cp mariadb:/etc/mysql/mariadb.conf.d/50-server.cnf ./srcs/requirements/mariadb/conf/.

      👉🏼 The command above will only work if the mariadb container is running. After copying the file to the host, you can pause the container, delete it, and remove the created image.

  • Customize MariaDB Configuration

    • Uncomment/Change the security and permission settings in the 50-server.cnf file copied from the MariaDB container that specify that MariaDB should run as the root user in the system:

      user                    = mysql #Original line
      user                    = root #Modified line

      👉🏼 The datadir and tmpdir configurations may be commented out and might need to be uncommented. Below is an explanation of what they do:

      • tmpdir → Specifies the directory where the MariaDB server stores its temporary files.
      • datadir → Specifies the directory where the MariaDB server stores its database files.
    • Change the security setting that specifies the IP address and Network where the MariaDB database server listens for connections from localhost to the local network in the 50-server.cnf file copied from the MariaDB container:

      bind-address                    = 127.0.0.1 #Original line
      bind-address                    = mariadb #Modified line
    • Add the command to copy the modified 50-server.cnf configuration file into the /etc/mysql/mariadb.conf.d/ directory in the MariaDB container to replace the original file with the customized one in the Dockerfile for MariaDB:

      FROM debian:bullseye 
      RUN apt update && apt upgrade -y && apt install -y mariadb-server
      COPY conf/50-server.cnf /etc/mysql/mariadb.conf.d/.
      ENTRYPOINT ["mysqld_safe"]
    • Add the command to create the /run/mysqld directory, which will be responsible for storing temporary files and important files for running MariaDB, such as the PID file and connection socket in the Dockerfile for MariaDB:

      FROM debian:bullseye 
      RUN apt update && apt upgrade -y && apt install -y mariadb-server
      COPY conf/50-server.cnf /etc/mysql/mariadb.conf.d/.
      RUN mkdir /run/mysqld
      ENTRYPOINT ["mysqld_safe"]

      👉🏼 To verify the need for the /run/mysqld directory, while the MariaDB container is running, you can check the contents of the /etc/mysql/mariadb.cnf file with the docker exec -it mariadb cat /etc/mysql/mariadb.cnf command and verify that there is a line describing that the connection socket file will be stored at the path /run/mysqld/mysqld.sock, which makes it necessary to have the /run/mysqld directory, which (probably) is not created automatically during the MariaDB installation.

      👉🏼 At this stage, you can test the functionality of the containers by rebuilding the images and starting them all. Finally, you can enter the MariaDB container with the docker exec -it mariadb /bin/bash command and enter the MariaDB CLI through the mysql -u root -p command to navigate the data and verify the creation of the database and tables.

    ⏮️ Previous
    Next ⏭️

Clone this wiki locally