From 9708715bb50823bef597fb612120084cf8e6a195 Mon Sep 17 00:00:00 2001 From: hugo gogo Date: Sun, 18 Sep 2022 16:39:13 +0200 Subject: [PATCH] mariadb and nginx works both with debian and alpine + makefile a little clean up --- Makefile | 55 +++------------- srcs/requirements/mariadb/Dockerfile | 62 +++++++++++++------ .../{50-server.cnf => 50-server.cnf.debian} | 9 +++ srcs/requirements/mariadb/conf/create_db.sql | 5 ++ .../mariadb/conf/mariadb-server.cnf.alpine | 62 +++++++++++++++++++ srcs/requirements/nginx/Dockerfile | 4 +- .../{nginx_alpine.conf => nginx.conf.alpine} | 0 .../{nginx_debian.conf => nginx.conf.debian} | 0 .../wordpress/conf/wp_entrypoint.sh | 4 ++ 9 files changed, 134 insertions(+), 67 deletions(-) rename srcs/requirements/mariadb/conf/{50-server.cnf => 50-server.cnf.debian} (96%) create mode 100644 srcs/requirements/mariadb/conf/create_db.sql create mode 100644 srcs/requirements/mariadb/conf/mariadb-server.cnf.alpine rename srcs/requirements/nginx/conf/{nginx_alpine.conf => nginx.conf.alpine} (100%) rename srcs/requirements/nginx/conf/{nginx_debian.conf => nginx.conf.debian} (100%) diff --git a/Makefile b/Makefile index a969dd3..e3222b5 100644 --- a/Makefile +++ b/Makefile @@ -24,32 +24,17 @@ RESET = "\e[0m" COMPOSE = ./srcs/docker-compose.yml -IMAGES = nginx \ - mariadb \ - wordpress - HOME_D = $(shell echo $(HOME)) VOLUMES_D = $(VOLUMES:%=$(HOME_D)/%) -VOLUMES = v_wp_site \ - v_wp_db - -CONTAINERS = $(IMAGES:%=my%) - -CONT = mytest SUDO = -# for rule super-clean, see : https://stackoverflow.com/questions/10024279/how-to-use-shell-commands-in-makefile -STOP = $(shell $(SUDO) docker ps -q) +# list of running containers, see : https://stackoverflow.com/questions/10024279/how-to-use-shell-commands-in-makefile +RUNNING = $(shell $(SUDO) docker ps -q) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # -# use 'make' to launch and relaunch the project # -# use 'make re' to relaunch and clean the dungling images # -# use 'make fre' to start all over again (images) # -# use 'make super-clean' if you want to delete all dockers on com- # -# puter even if not related to the project # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # all: build $(VOLUMES_D) up @@ -73,38 +58,18 @@ list: @$(SUDO) docker volume ls @echo "" -# remove project images -rm-images: - $(SUDO) docker image rm -f $(IMAGES) - -# stop project containers -stop: - - $(SUDO) docker stop $(CONTAINERS) - -# remove all stopped containers and dangling images (dangling images, see : https://projectatomic.io/blog/2015/07/what-are-docker-none-none-images/) -prune: +# remove project images and containers not used +clean: + - $(SUDO) docker stop $(RUNNING) $(SUDO) docker system prune -f -# remove project images and containers -clean: stop prune - -# remove project volumes -rm-volumes: - - $(SUDO) docker volume rm -f $(VOLUMES) - /bin/rm -rf $(VOLUMES_D) - -# remove project -fclean: rm-images clean rm-volumes - -# remove all dockers, even not related to the project -super-clean: - - $(SUDO) docker stop $(STOP) +# remove every dockers +fclean: + - $(SUDO) docker stop $(RUNNING) $(SUDO) docker system prune -af --volumes /bin/rm -rf $(VOLUMES_D) -re: clean all +re: fclean all -fre: fclean all - -.PHONY : all build up $(VOLUMES_D) list rm-images stop rm-containers close-nginx prune clean rm-volumes fclean super-clean re fre +.PHONY : all $(VOLUMES_D) build up list clean fclean re diff --git a/srcs/requirements/mariadb/Dockerfile b/srcs/requirements/mariadb/Dockerfile index 509da08..e1955e3 100644 --- a/srcs/requirements/mariadb/Dockerfile +++ b/srcs/requirements/mariadb/Dockerfile @@ -1,26 +1,48 @@ -FROM debian:buster +# debian ----------------------------------------------------- -ARG DB_NAME -ARG DB_USER -ARG DB_PSWD +# FROM debian:buster +# +# ARG DB_NAME +# ARG DB_USER +# ARG DB_PSWD +# +# RUN apt update && apt install -y \ +# mariadb-client \ +# mariadb-server \ +# && \ +# rm -rf /var/lib/apt/lists/* +# +# # config file .cnf : +# COPY ./conf/50-server.cnf.debian /etc/mysql/mariadb.conf.d/50-server.cnf -RUN apt update && apt install -y \ - mariadb-client \ - mariadb-server -RUN rm -rf /var/lib/apt/lists/* +# alpine ----------------------------------------------------- + + FROM alpine:3.15 + + ARG DB_NAME + ARG DB_USER + ARG DB_PSWD + + # vim and bash for debug + RUN apk update && apk add \ + mariadb \ + mariadb-client \ + vim bash \ + && \ + rm -rf /var/cache/apk* + + # config file .cnf : + COPY ./conf/mariadb-server.cnf.alpine /etc/my.cnf.d/mariadb-server.cnf + +# common ----------------------------------------------------- + +# init mysql database +RUN mysql_install_db --user=mysql --ldata=/var/lib/mysql && \ + mkdir -p /var/run/mysqld && \ + chown -R mysql:root /var/run/mysqld # configure wp database -RUN service mysql start && \ - mariadb --execute="CREATE DATABASE ${DB_NAME};" && \ - mariadb --execute="CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PSWD}';" && \ - mariadb --execute="GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' with grant option;" +COPY ./conf/create_db.sql /usr/bin/create_db.sql -# # config file 50-server.cnf : -# # uncomment port = 3306 -# RUN sed -i "s/^#port/port /g" /etc/mysql/mariadb.conf.d/50-server.cnf -# # comment bind-address = 127.0.0.1 -# RUN sed -i "s/^bind-address /#bind-address/g" /etc/mysql/mariadb.conf.d/50-server.cnf -COPY ./conf/50-server.cnf ./ - -ENTRYPOINT [ "mysqld" ] +ENTRYPOINT [ "mysqld", "--user=mysql", "--init-file=/usr/bin/create_db.sql" ] diff --git a/srcs/requirements/mariadb/conf/50-server.cnf b/srcs/requirements/mariadb/conf/50-server.cnf.debian similarity index 96% rename from srcs/requirements/mariadb/conf/50-server.cnf rename to srcs/requirements/mariadb/conf/50-server.cnf.debian index 6dd17f7..240c501 100644 --- a/srcs/requirements/mariadb/conf/50-server.cnf +++ b/srcs/requirements/mariadb/conf/50-server.cnf.debian @@ -1,3 +1,12 @@ +# inception modifications : +# +# [mysqld] : +# < port = 3306 +# > #port = 3306 +# --- +# < #bind-address = 127.0.0.1 +# > bind-address = 127.0.0.1 + # # These groups are read by MariaDB server. # Use it for options that only the server (but not clients) should see diff --git a/srcs/requirements/mariadb/conf/create_db.sql b/srcs/requirements/mariadb/conf/create_db.sql new file mode 100644 index 0000000..0067492 --- /dev/null +++ b/srcs/requirements/mariadb/conf/create_db.sql @@ -0,0 +1,5 @@ +USE mysql; + +CREATE DATABASE IF NOT EXISTS ${DB_NAME}; +CREATE USER IF NOT EXISTS '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PSWD}'; +GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' with grant option; diff --git a/srcs/requirements/mariadb/conf/mariadb-server.cnf.alpine b/srcs/requirements/mariadb/conf/mariadb-server.cnf.alpine new file mode 100644 index 0000000..84903f2 --- /dev/null +++ b/srcs/requirements/mariadb/conf/mariadb-server.cnf.alpine @@ -0,0 +1,62 @@ +# https://wiki.alpinelinux.org/wiki/MariaDB +# inception modifications : +# +# [mysqld] : +# < skip-networking = false +# > skip-networking +# --- +# < datadir = /var/lib/mysql +# > +# --- +# < port = 3306 +# > +# --- +# < socket=/var/lib/mysql/mysql.sock +# > + + +# +# These groups are read by MariaDB server. +# Use it for options that only the server (but not clients) should see + +# this is read by the standalone daemon and embedded servers +[server] + +# this is only for the mysqld standalone daemon +[mysqld] +skip-networking = false +datadir = /var/lib/mysql +port = 3306 +socket=/var/lib/mysql/mysql.sock + +# Galera-related settings +[galera] +# Mandatory settings +#wsrep_on=ON +#wsrep_provider= +#wsrep_cluster_address= +#binlog_format=row +#default_storage_engine=InnoDB +#innodb_autoinc_lock_mode=2 +# +# Allow server to accept connections on all interfaces. +# +#bind-address=0.0.0.0 +# +# Optional setting +#wsrep_slave_threads=1 +#innodb_flush_log_at_trx_commit=0 + +# this is only for embedded server +[embedded] + +# This group is only read by MariaDB servers, not by MySQL. +# If you use the same .cnf file for MySQL and MariaDB, +# you can put MariaDB-only options here +[mariadb] + +# This group is only read by MariaDB-10.5 servers. +# If you use the same .cnf file for MariaDB of different versions, +# use this group for options that older servers don't understand +[mariadb-10.5] + diff --git a/srcs/requirements/nginx/Dockerfile b/srcs/requirements/nginx/Dockerfile index 337cb5d..44eb4ca 100644 --- a/srcs/requirements/nginx/Dockerfile +++ b/srcs/requirements/nginx/Dockerfile @@ -10,7 +10,7 @@ # rm -rf /var/lib/apt/lists/* # # # nginx conf -# COPY ./conf/nginx_debian.conf /etc/nginx/nginx.conf +# COPY ./conf/nginx.conf.debian /etc/nginx/nginx.conf # COPY ./conf/inception_nginx.conf /etc/nginx/conf.d/ # alpine (~ 45s) --------------------------------------------- @@ -25,7 +25,7 @@ rm -rf /var/cache/apk* # nginx conf - COPY ./conf/nginx_alpine.conf /etc/nginx/nginx.conf + COPY ./conf/nginx.conf.alpine /etc/nginx/nginx.conf COPY ./conf/inception_nginx.conf /etc/nginx/http.d/ # common ----------------------------------------------------- diff --git a/srcs/requirements/nginx/conf/nginx_alpine.conf b/srcs/requirements/nginx/conf/nginx.conf.alpine similarity index 100% rename from srcs/requirements/nginx/conf/nginx_alpine.conf rename to srcs/requirements/nginx/conf/nginx.conf.alpine diff --git a/srcs/requirements/nginx/conf/nginx_debian.conf b/srcs/requirements/nginx/conf/nginx.conf.debian similarity index 100% rename from srcs/requirements/nginx/conf/nginx_debian.conf rename to srcs/requirements/nginx/conf/nginx.conf.debian diff --git a/srcs/requirements/wordpress/conf/wp_entrypoint.sh b/srcs/requirements/wordpress/conf/wp_entrypoint.sh index 9fb752f..6ebd701 100644 --- a/srcs/requirements/wordpress/conf/wp_entrypoint.sh +++ b/srcs/requirements/wordpress/conf/wp_entrypoint.sh @@ -4,6 +4,10 @@ mkdir -p ${WP_DIR} wp core download --path="${WP_DIR}" --allow-root +# mariadb --execute="CREATE DATABASE ${DB_NAME};" && \ +# mariadb --execute="CREATE USER '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PSWD}';" && \ +# mariadb --execute="GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO '${DB_USER}'@'localhost' with grant option;" + ## create config file : wp config create \ --dbhost=mariadb \