Skip to content

Volumes

Iva edited this page Aug 23, 2024 · 9 revisions
  • Configuration Refinement (DB Volume)

    • Add a volume to the docker-compose.yml file responsible for gathering the database data to be accessed by the MariaDB container. Link the directory where the volume will be stored with the /var/lib/mysql directory in each container:

      version: "3.8"
      
      services:
        nginx:
          build: requirements/nginx/.
          container_name: nginx
          restart: on-failure
          depends_on:
            - wordpress
          networks:
            - inception
          ports:
            - "443:443"
          volumes:
            - .web:/var/www/html
        wordpress:
          build: requirements/wordpress/.
          container_name: wordpress
          restart: on-failure
          depends_on:
            - mariadb
          networks:
            - inception
          expose:
            - "9000"
          volumes:
            - .web:/var/www/html
        mariadb:
          build: requirements/mariadb/.
          container_name: mariadb
          restart: on-failure
          networks:
            - inception
          expose:
            - "3306"
          volumes:
            - .db:/var/lib/mysql
      networks:
        inception:
          driver: bridge

      👉🏼 The directory structure created by the docker-compose.yml file will be modified, adding the rootInception/srcs/db/ directory if it does not exist, and considering it if it does exist. This directory will be responsible for storing the volume to be accessed by MariaDB.

      👉🏼 The project requirements specify that the volume directory should be located at /home/login/data/ within the host. For this reason, the .db directory will be renamed to the required path later on.

    • Create a script file mariadb-run.sh in the rootInception/srcs/requirements/mariadb/tools/ path that will be executed last in the Dockerfile of the MariaDB container. This file will be responsible for configuring the necessary files and directories to initialize the database, and finally, running MariaDB in the terminal. The file should be filled as described below:

      #!/bin/bash
      mysql_install_db 
      mysqld
    • Add the command to copy the script created above to the local directory and change the permissions so that it can be executed at the end of the Dockerfile in the Dockerfile for MariaDB, as shown below:

      FROM debian:bullseye 
      RUN apt update && apt upgrade -y && apt install -y mariadb-server
      COPY conf/50-server.cnf /etc/mysql/mariadb.conf.d/.
      COPY conf/init.sql /etc/mysql/init.sql
      RUN mkdir /run/mysqld
      COPY tools/mariadb-run.sh .
      RUN chmod +x ./mariadb-run.sh
      ENTRYPOINT ["./mariadb-run.sh"]
  • Configuration Refinement (Rename Volumes)

    • Change the volume directories to the one specified in the project requirements with the home/login/data structure and add the volume configuration in the docker-compose.yml file, as shown below:

      version: "3.8"
      
      services:
        nginx:
          build: requirements/nginx/.
          container_name: nginx
          restart: on-failure
          depends_on:
            - wordpress
          networks:
            - inception
          ports:
            - "443:443"
          volumes:
            - v_web:/var/www/html
        wordpress:
          build: requirements/wordpress/.
          container_name: wordpress
          restart: on-failure
          depends_on:
            - mariadb
          networks:
            - inception
          expose:
            - "9000"
          volumes:
            - v_web:/var/www/html
        mariadb:
          build: requirements/mariadb/.
          container_name: mariadb
          restart: on-failure
          networks:
            - inception
          expose:
            - "3306"
          volumes:
            - v_db:/var/lib/mysql
      volumes:
        v_web:
          driver: local
          driver_opts:
            type: none
            o: bind
            device: ./home/ivbatist/data/wordpress
        v_db:
          driver: local
          driver_opts:
            type: none
            o: bind
            device: ./home/ivbatist/data/mariadb
      networks:
        inception:
          driver: bridge

      👉🏼 The directory structure defined as the device in the volume configuration must already exist to start the volumes correctly.

    ⏮️ Previous
    Next ⏭️

Clone this wiki locally