dev: mariadb users *

other things also
This commit is contained in:
mcolonna 2025-06-20 12:33:59 +02:00
parent 39f90db91d
commit a1d2d83f60
11 changed files with 79 additions and 28 deletions

View file

@ -4,9 +4,11 @@ DOCKER=docker
MKTEMP=mktemp MKTEMP=mktemp
include .env include .env
export DATABASE_PWD
export DATABASE_PWD_ROOT
export VOLUMES_PATH
SRC_COMPOSE=srcs/ SRC_COMPOSE=srcs/
SRC_WWW_MORE=srcs/www/
CERT_PATH=srcs/__cert/ CERT_PATH=srcs/__cert/
CERT_PATH_FILES=$(addprefix $(CERT_PATH), ca.pem cert.key cert.crt ) CERT_PATH_FILES=$(addprefix $(CERT_PATH), ca.pem cert.key cert.crt )
@ -33,7 +35,7 @@ run : $(CERT_PATH_FILES)
cd -- $(SRC_COMPOSE) cd -- $(SRC_COMPOSE)
mkdir -p $(VOLUMES_PATH)/www mkdir -p $(VOLUMES_PATH)/www
mkdir -p $(VOLUMES_PATH)/db mkdir -p $(VOLUMES_PATH)/db
DATABASE_PWD="$(DATABASE_PWD)" VOLUMES_PATH="$(VOLUMES_PATH)" $(DOCKER) compose up --build $(DOCKER) compose up --build
>/dev/null cd - >/dev/null cd -
@ -41,7 +43,9 @@ run : $(CERT_PATH_FILES)
reset : reset :
@$(_ECHO) @$(_ECHO)
echoo "Removing all data..." echoo "Resetting data..."
rm -f $(VOLUMES_PATH)/www/.installed
rm -f $(VOLUMES_PATH)/db/.installed
cd -- $(SRC_COMPOSE) cd -- $(SRC_COMPOSE)
docker compose down -v docker compose down -v
>/dev/null cd - >/dev/null cd -

View file

@ -1,3 +1,4 @@
DOMAIN=mcolonna.42.fr DOMAIN=mcolonna.42.fr
DATABASE_PWD=[insert_cool_password_here] DATABASE_PWD=[insert_cool_password_here]
DATABASE_PWD_ROOT=[insert_other_cool_password_here]
VOLUMES_PATH=[insert_absolute_path] VOLUMES_PATH=[insert_absolute_path]

View file

@ -34,6 +34,7 @@ services:
- db:/db:rw - db:/db:rw
secrets: secrets:
- database-pwd - database-pwd
- database-pwd-root
networks: networks:
- network - network
restart: always restart: always
@ -55,6 +56,8 @@ volumes:
secrets: secrets:
database-pwd: database-pwd:
environment: "DATABASE_PWD" environment: "DATABASE_PWD"
database-pwd-root:
environment: "DATABASE_PWD_ROOT"
networks: networks:
network: network:

View file

@ -1,15 +1,17 @@
FROM alpine:3.21.3 FROM alpine:3.21.3
RUN apk update RUN apk update
RUN apk add mariadb RUN apk add mariadb mariadb-client
RUN apk fix RUN apk fix
RUN rm -rf /etc/my.cnf.d/ RUN rm -rf /etc/my.cnf.d/
RUN mkdir -p /etc/my.cnf.d/ RUN mkdir -p /etc/my.cnf.d/
COPY conf/mariadb-server.cnf /etc/my.cnf.d/mariadb-server.cnf COPY conf/mariadb-server.cnf /etc/my.cnf.d/mariadb-server.cnf
COPY run.sh /run.sh COPY run.sh /run.sh
COPY ismariadbrunning.sh /ismariadbrunning.sh
COPY healthcheck.sh /healthcheck.sh
RUN addgroup -S www && adduser -S www www RUN addgroup -S db && adduser -S db db
RUN mkdir /db RUN mkdir /db
RUN chmod -R 666 /db RUN chmod -R 666 /db
@ -18,4 +20,4 @@ EXPOSE 3306
# start # start
CMD ["/run.sh"] CMD ["/run.sh"]
HEALTHCHECK --interval=1s --timeout=10s --start-period=600s --retries=1 CMD [ "sh", "-c", "! [ -z \"$(netstat -tuln | grep :3306)\" ]" ] HEALTHCHECK --interval=1s --timeout=10s --start-period=600s --retries=1 CMD [ "/healthcheck.sh" ]

View file

@ -1,10 +1,6 @@
[client-server] [client-server]
socket=/tmp/mysql.sock socket=/run/mysql.sock
port=3306 port=3306
[mysqld]
# fix from https://stackoverflow.com/questions/75696472/aborted-connection-3-to-db-unconnected-user-unauthenticated-host-172-21
skip-grant-tables
[mariadb] [mariadb]
datadir=/db datadir=/db

View file

@ -0,0 +1,5 @@
#!/bin/sh
set -e
/ismariadbrunning.sh && [ -f /db/.dockerhealthcheck ]
exit $?

View file

@ -0,0 +1,5 @@
#!/bin/sh
set -e
! [ -z "$(netstat -tuln | grep :3306)" ]
exit $?

View file

@ -1,29 +1,59 @@
#!/bin/sh #!/bin/sh
set -e set -e
echo sql_quote()
dirisempty()
{ {
[ -z "$( ls -A "$1" )" ] echo "SELECT QUOTE(FROM_BASE64('$( echo -n "$1" | base64 )'));" | mariadb -u root -N
} }
# install database if doesn't exist rm -f /db/.dockerhealthcheck
if dirisempty /db
then
echo "database doesn't exist. creating it..."
echo echo
# install database if doesn't exist
if ! [ -f /db/.installed ]
then
echo "database doesn't exist."
echo
echo ">>> clean..."
rm -rf -- $(find /db -mindepth 1 -maxdepth 1)
echo ">>> creating database..."
chmod -R 777 /db chmod -R 777 /db
mariadb-install-db --user=www --datadir=/db mariadb-install-db --user=db --datadir=/db
echo "creating database..." echo
echo ">>> running mariadbd..."
DATABASE_PWD="$(cat /run/secrets/database-pwd)" DATABASE_PWD="$(cat /run/secrets/database-pwd)"
echo ' DATABASE_PWD_ROOT="$(cat /run/secrets/database-pwd-root)"
# run mariadbd and wait for it to be working
mariadbd -u root &
while ! /ismariadbrunning.sh && kill -0 $!
do sleep 1; done
if ! kill -0 $!
then
echo
echo "failed when running mariadbd :("
exit 1
fi
echo
echo ">>> configure..."
echo "
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
CREATE DATABASE wp; CREATE DATABASE wp;
GRANT ALL PRIVILEGES ON wp.* TO www IDENTIFIED BY FROM_BASE64("'$(echo $DATABASE_PWD | base64)'"); GRANT ALL PRIVILEGES ON wp.* TO db IDENTIFIED BY $( sql_quote "$DATABASE_PWD" );
ALTER USER root@localhost IDENTIFIED BY $( sql_quote "$DATABASE_PWD_ROOT" );
FLUSH PRIVILEGES; FLUSH PRIVILEGES;
' | mariadbd -u root --bootstrap " | mariadb -u root
kill $!
echo echo
echo ">>> kill mariadbd..."
>/db/.installed echo "if this file exists, that means the database is entirely installed."
echo
echo "database created!" echo "database created!"
echo echo
fi fi
@ -32,4 +62,5 @@ echo "to recreate a new database, remove the db volume of this compose."
echo echo
# run mariadb # run mariadb
touch /db/.dockerhealthcheck
mariadbd -u root mariadbd -u root

View file

@ -27,4 +27,4 @@ COPY conf/ /etc/nginx/
# start # start
CMD ["nginx", "-g", "daemon off;"] CMD ["nginx", "-g", "daemon off;"]
HEALTHCHECK --interval=1s --timeout=10s --start-period=60s --retries=1 CMD [ "sh", "-c", "! [ -z \"$(netstat -tuln | grep :443)\" ]" ] HEALTHCHECK --interval=1s --timeout=10s --start-period=60s --retries=1 CMD [ "/ismariarunning.sh" ]

View file

@ -23,10 +23,10 @@
define( 'DB_NAME', 'wp' ); define( 'DB_NAME', 'wp' );
/** Database username */ /** Database username */
define( 'DB_USER', 'www' ); define( 'DB_USER', 'db' ); # TODO
/** Database password */ /** Database password */
define( 'DB_PASSWORD', `cat /run/secrets/database-pwd` ); define( 'DB_PASSWORD', `cat /run/secrets/database-pwd` ); # TODO
/** Database hostname */ /** Database hostname */
define( 'DB_HOST', 'mariadb:3306' ); define( 'DB_HOST', 'mariadb:3306' );
@ -85,7 +85,7 @@ $table_prefix = 'wp_';
* *
* @link https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/ * @link https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/
*/ */
define( 'WP_DEBUG', false ); define( 'WP_DEBUG', true ); # TODO
/* Add any custom values between this line and the "stop editing" line. */ /* Add any custom values between this line and the "stop editing" line. */

View file

@ -8,10 +8,13 @@ dirisempty()
[ -z "$( ls -A "$1" )" ] [ -z "$( ls -A "$1" )" ]
} }
if dirisempty /www if ! [ -f /www/.installed ]
then then
echo "download and uncompress wordpress release..." echo "clean..."
rm -rf -- $(find /www -mindepth 1 -maxdepth 1)
echo echo
echo "download and uncompress wordpress release..."
apk add curl apk add curl
apk fix apk fix
rm -rf /build rm -rf /build
@ -24,6 +27,7 @@ then
echo "add files from /conf/wwwmore/" echo "add files from /conf/wwwmore/"
echo echo
cp -r /conf/wwwmore/. /www cp -r /conf/wwwmore/. /www
>/www/.installed echo "if this file exists, that means the database is entirely installed."
echo "www directory created!" echo "www directory created!"
echo echo
fi fi