diff --git a/Makefile b/Makefile index 5df0ee9..58f7454 100644 --- a/Makefile +++ b/Makefile @@ -6,17 +6,60 @@ # By: whaffman +#+ # # +#+ # # Created: 2025/05/12 15:33:56 by whaffman #+# #+# # -# Updated: 2025/05/12 15:35:21 by whaffman ######## odam.nl # +# Updated: 2025/05/13 08:59:04 by whaffman ######## odam.nl # # # # **************************************************************************** # +DOCKER_COMPOSE = docker compose +DOCKER_COMPOSE_FILE = ./srcs/docker-compose.yml +DC = $(DOCKER_COMPOSE) -f $(DOCKER_COMPOSE_FILE) + +.PHONY: all build up down remove + +all: build up +build: + @echo "$(gub)Building Docker containers...$(reset)" + $(DC) build up: - docker-compose up -d --build + @echo "$(gub)Starting Docker containers...$(reset)" + $(DC) up -d --build down: - docker-compose down + @echo "$(gub)Stopping Docker containers...$(reset)" + $(DC) down -remove: - docker-compose down --rmi all - @docker volume rm $(shell docker volume ls -qf dangling=true) || true +clean: + @echo "$(gub)Cleaning up Docker containers...$(reset)" + $(DC) down --rmi local --volumes --remove-orphans +logs: + @echo "$(gub)Displaying logs...$(reset)" + @$(DC) logs + +ps: + @echo "$(gub)Displaying running containers...$(reset)" + @$(DC) ps + @echo "$(gub)Displaying networks...$(reset)" + @docker network ls + @echo "$(gub)Displaying volumes...$(reset)" + @docker volume ls + @echo "$(gub)Displaying images...$(reset)" + @docker images + @echo "$(gub)Displaying all containers...$(reset)" + @docker ps -a + + +remove: + @echo "$(gub)Removing Docker containers...$(reset)" + @$(DC) rm -f + @$(DC) volume rm -f + @$(DC) network rm -f + + +# Colors + +green:=$(shell tput setaf 2) +bold:=$(shell tput bold) +uncerline:=$(shell tput smul) +gub:=$(green)$(underline)$(bold) +reset:=$(shell tput sgr0) diff --git a/secrets/db_password.txt b/secrets/db_password.txt index e69de29..30d74d2 100644 --- a/secrets/db_password.txt +++ b/secrets/db_password.txt @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/secrets/db_root_password.txt b/secrets/db_root_password.txt index e69de29..30d74d2 100644 --- a/secrets/db_root_password.txt +++ b/secrets/db_root_password.txt @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/srcs/.env b/srcs/.env deleted file mode 100644 index e69de29..0000000 diff --git a/srcs/docker-compose.yml b/srcs/docker-compose.yml index 847ce21..024c741 100644 --- a/srcs/docker-compose.yml +++ b/srcs/docker-compose.yml @@ -1,20 +1,8 @@ -version: '3.8' - -env_file: - - .env - -secrets: - mariadb_root_password: - file: ./secrets/db_root_password - mariadb_user_password: - file: ./secrets/db_password - wordpress_admin_password: - file: ./requirements/wordpress/.env - wordpress_user_password: - file: ./requirements/wordpress/.env +name: inception services: mariadb: + container_name: mariadb restart: always build: context: ./requirements/mariadb @@ -25,41 +13,60 @@ services: - docker-network volumes: - data_mariadb:/var/lib/mysql + secrets: + - mariadb_root_password + - mariadb_user_password + env_file: + - ./requirements/mariadb/.env + - nginx: - restart: always - build: - context: ./requirements/nginx - dockerfile: Dockerfile - ports: - -'443:443' - networks: - - docker-network - volumes: - - data_wordpress:/var/www/html - - wordpress: - restart: always - build: - context: ./requirements/wordpress - dockerfile: Dockerfile - depends_on: - - mariadb - ports: - - '9000:9000' - networks: - - docker-network - volumes: - - data_wordpress:/var/www/html + # wordpress: + # container_name: wordpress + # restart: always + # build: + # context: ./requirements/wordpress + # dockerfile: Dockerfile + # depends_on: + # - mariadb + # ports: + # - '9000:9000' + # networks: + # - docker-network + # volumes: + # - data_wordpress:/var/www/html + # nginx: + # container_name: nginx + # restart: always + # build: + # context: ./requirements/nginx + # dockerfile: Dockerfile + # depends_on: + # - wordpress + # ports: + # - '443:443' + # networks: + # - docker-network + # volumes: + # - data_wordpress:/var/www/html networks: docker-network: - + driver: bridge volumes: data_mariadb: driver: local - data_wordpress: - driver: local + # data_wordpress: + # driver: local + +secrets: + mariadb_root_password: + file: ../secrets/db_root_password.txt + mariadb_user_password: + file: ../secrets/db_password.txt + # wordpress_admin_password: + # file: ./requirements/wordpress/.env + # wordpress_user_password: + # file: ./requirements/wordpress/.env \ No newline at end of file diff --git a/srcs/requirements/mariadb/.env b/srcs/requirements/mariadb/.env new file mode 100644 index 0000000..045d30f --- /dev/null +++ b/srcs/requirements/mariadb/.env @@ -0,0 +1,4 @@ +MYSQL_ROOT_PASSWORD=123456 +MYSQL_DATABASE=wordpress +MYSQL_USER=wordpress +MYSQL_PASSWORD=123456 \ No newline at end of file diff --git a/srcs/requirements/mariadb/Dockerfile b/srcs/requirements/mariadb/Dockerfile index eadd107..c97c496 100644 --- a/srcs/requirements/mariadb/Dockerfile +++ b/srcs/requirements/mariadb/Dockerfile @@ -1,3 +1,44 @@ FROM alpine:3.20 -RUN apk add --no-cache mariadb mariadb-client +RUN apk update && apk upgrade && \ + apk add --no-cache \ + mariadb \ + mariadb-client \ + mariadb-server-utils && \ + rm -rf /var/cache/apk/* + +# Create a new user and group +RUN addgroup -S mariadb && adduser -S mariadb -G mariadb +# Create the data directory +RUN mkdir -p /var/lib/mysql && \ + chown -R mariadb:mariadb /var/lib/mysql && \ + chmod 700 /var/lib/mysql +# Create the socket directory +RUN mkdir -p /var/run/mysqld && \ + chown -R mariadb:mariadb /var/run/mysqld && \ + chmod 755 /var/run/mysqld +# Create the configuration directory +# Set the working directory +WORKDIR /var/lib/mysql +# Expose the MySQL port +EXPOSE 3306 +# Set the default environment variables + +# Copy the configuration file +COPY ./conf/my.cnf /etc/my.cnf +# Make the configuration file readable by the user +RUN chown mariadb:mariadb /etc/my.cnf + + +# Copy the entrypoint script +COPY ./tools/entrypoint.sh /usr/local/bin/entrypoint.sh +# Make the entrypoint script executable +RUN chmod +x /usr/local/bin/entrypoint.sh +# Set the default user to run the container +USER mariadb + +# # Set the entrypoint script +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] +# Set the default command to run when the container starts +CMD ["mysqld", "--bind-address=0.0.0.0"] + diff --git a/srcs/requirements/mariadb/conf/my.cnf b/srcs/requirements/mariadb/conf/my.cnf new file mode 100644 index 0000000..343d14a --- /dev/null +++ b/srcs/requirements/mariadb/conf/my.cnf @@ -0,0 +1,6 @@ +[mysqld] +port = 3306 +datadir = /var/lib/mysql +socket = /var/run/mysqld/mysqld.sock +skip-networking = false +bind-address = 0.0.0.0 \ No newline at end of file diff --git a/srcs/requirements/mariadb/tools/entrypoint.sh b/srcs/requirements/mariadb/tools/entrypoint.sh new file mode 100644 index 0000000..d9e548a --- /dev/null +++ b/srcs/requirements/mariadb/tools/entrypoint.sh @@ -0,0 +1,32 @@ +#!/bin/sh +echo "Hello from mariadb entrypoint.sh" +echo db: $MYSQL_DATABASE + +mysql_install_db +if [ -d "/var/lib/mysql/$MYSQL_DATABASE" ] +then + + echo "Database already exists" +else + +# Set root option so that connexion without root password is not possible + + + + +#Add a root user on 127.0.0.1 to allow remote connexion +#Flush privileges allow to your sql tables to be updated automatically when you modify it +#mysql -uroot launch mysql command line client +echo "GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '$MYSQL_ROOT_PASSWORD'; FLUSH PRIVILEGES;" | mysql -uroot + +#Create database and user in the database for wordpress + +echo "CREATE DATABASE IF NOT EXISTS $MYSQL_DATABASE; GRANT ALL ON $MYSQL_DATABASE.* TO '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD'; FLUSH PRIVILEGES;" | mysql -u root + +#Import database in the mysql command line +# mysql -uroot -p$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE < /usr/local/bin/wordpress.sql + +fi + + +exec "$@" \ No newline at end of file diff --git a/srcs/requirements/nginx/Dockerfile b/srcs/requirements/nginx/Dockerfile index e69de29..b7c139b 100644 --- a/srcs/requirements/nginx/Dockerfile +++ b/srcs/requirements/nginx/Dockerfile @@ -0,0 +1,6 @@ +FROM alpine:3.20 + +RUN apk update && apk upgrade && \ + apk add --no-cache \ + nginx \ + openssl \ diff --git a/srcs/requirements/wordpress/Dockerfile b/srcs/requirements/wordpress/Dockerfile index e69de29..3994b33 100644 --- a/srcs/requirements/wordpress/Dockerfile +++ b/srcs/requirements/wordpress/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine:3.20 + +RUN apk update && apk upgrade \ No newline at end of file