From 4e3cc1dfe2a405fab2227aab032f7457468a4f44 Mon Sep 17 00:00:00 2001 From: Steven Guikal Date: Tue, 19 Oct 2021 14:55:43 -0400 Subject: [PATCH 1/3] feat(docker): only use mariadb socket for connections --- conf/config.dev | 6 +++--- docker-compose.yml | 17 ++++++++++------- docker/fastapi-entrypoint.sh | 10 ---------- docker/git-entrypoint.sh | 9 --------- docker/mariadb-init-entrypoint.sh | 10 ---------- docker/php-entrypoint.sh | 10 ---------- docker/test-mysql-entrypoint.sh | 7 ------- 7 files changed, 13 insertions(+), 56 deletions(-) diff --git a/conf/config.dev b/conf/config.dev index ec0b33dc..b8bd5bdb 100644 --- a/conf/config.dev +++ b/conf/config.dev @@ -10,12 +10,12 @@ backend = mysql ; If using sqlite, set name to the database file path. -name = aurweb_test +name = aurweb ; MySQL database information. User defaults to root for containerized ; testing with mysqldb. This should be set to a non-root user. -user = root -;password = non-root-user-password +user = aur +password = aur host = localhost ;port = 3306 socket = /var/run/mysqld/mysqld.sock diff --git a/docker-compose.yml b/docker-compose.yml index 6f8e0d25..55c3527d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -66,20 +66,19 @@ services: mariadb_init: image: aurweb:latest init: true - environment: - - DB_HOST=mariadb entrypoint: /docker/mariadb-init-entrypoint.sh command: echo "MariaDB tables initialized." depends_on: mariadb: condition: service_healthy + volumes: + - mariadb_run:/var/run/mysqld git: image: aurweb:latest init: true environment: - AUR_CONFIG=/aurweb/conf/config - - DB_HOST=mariadb entrypoint: /docker/git-entrypoint.sh command: /docker/scripts/run-sshd.sh ports: @@ -89,6 +88,8 @@ services: depends_on: mariadb_init: condition: service_started + volumes: + - mariadb_run:/var/run/mysqld smartgit: image: aurweb:latest @@ -138,7 +139,6 @@ services: init: true environment: - AUR_CONFIG=/aurweb/conf/config - - DB_HOST=mariadb entrypoint: /docker/php-entrypoint.sh command: /docker/scripts/run-php.sh healthcheck: @@ -150,6 +150,8 @@ services: condition: service_healthy memcached: condition: service_healthy + volumes: + - mariadb_run:/var/run/mysqld ports: - "19000:9000" @@ -158,7 +160,6 @@ services: init: true environment: - AUR_CONFIG=conf/config - - DB_HOST=mariadb - FASTAPI_WORKERS=${FASTAPI_WORKERS} entrypoint: /docker/fastapi-entrypoint.sh command: /docker/scripts/run-fastapi.sh "${FASTAPI_BACKEND}" @@ -171,6 +172,8 @@ services: condition: service_healthy redis: condition: service_healthy + volumes: + - mariadb_run:/var/run/mysqld ports: - "18000:8000" @@ -228,7 +231,6 @@ services: init: true environment: - AUR_CONFIG=conf/config - - DB_HOST=mariadb entrypoint: /docker/test-mysql-entrypoint.sh command: /docker/scripts/run-pytests.sh clean stdin_open: true @@ -237,6 +239,7 @@ services: mariadb_init: condition: service_started volumes: + - mariadb_run:/var/run/mysqld - git_data:/aurweb/aur.git - ./cache:/cache - ./aurweb:/aurweb/aurweb @@ -274,7 +277,6 @@ services: init: true environment: - AUR_CONFIG=conf/config - - DB_HOST=mariadb entrypoint: /docker/tests-entrypoint.sh command: setup-sqlite.sh run-tests.sh stdin_open: true @@ -283,6 +285,7 @@ services: mariadb_init: condition: service_started volumes: + - mariadb_run:/var/run/mysqld - git_data:/aurweb/aur.git - ./cache:/cache - ./aurweb:/aurweb/aurweb diff --git a/docker/fastapi-entrypoint.sh b/docker/fastapi-entrypoint.sh index 3829b0bf..9dba5562 100755 --- a/docker/fastapi-entrypoint.sh +++ b/docker/fastapi-entrypoint.sh @@ -1,19 +1,9 @@ #!/bin/bash set -eou pipefail -[[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1 - -DB_NAME="aurweb" -DB_USER="aur" -DB_PASS="aur" - # Setup a config for our mysql db. cp -vf conf/config.dev conf/config sed -i "s;YOUR_AUR_ROOT;$(pwd);g" conf/config -sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" conf/config -sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" conf/config -sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" conf/config -sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" conf/config sed -ri "s;^(aur_location) = .+;\1 = https://localhost:8444;" conf/config diff --git a/docker/git-entrypoint.sh b/docker/git-entrypoint.sh index f07a5577..3fee426a 100755 --- a/docker/git-entrypoint.sh +++ b/docker/git-entrypoint.sh @@ -38,18 +38,9 @@ Match User aur AcceptEnv AUR_OVERWRITE EOF -DB_NAME="aurweb" -DB_HOST="mariadb" -DB_USER="aur" -DB_PASS="aur" - # Setup a config for our mysql db. cp -vf conf/config.dev $AUR_CONFIG sed -i "s;YOUR_AUR_ROOT;$(pwd);g" $AUR_CONFIG -sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" $AUR_CONFIG -sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" $AUR_CONFIG -sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" $AUR_CONFIG -sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" $AUR_CONFIG AUR_CONFIG_DEFAULTS="${AUR_CONFIG}.defaults" diff --git a/docker/mariadb-init-entrypoint.sh b/docker/mariadb-init-entrypoint.sh index 4cd6f46c..413227b9 100755 --- a/docker/mariadb-init-entrypoint.sh +++ b/docker/mariadb-init-entrypoint.sh @@ -1,19 +1,9 @@ #!/bin/bash set -eou pipefail -[[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1 - -DB_NAME="aurweb" -DB_USER="aur" -DB_PASS="aur" - # Setup a config for our mysql db. cp -vf conf/config.dev conf/config sed -i "s;YOUR_AUR_ROOT;$(pwd);g" conf/config -sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" conf/config -sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" conf/config -sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" conf/config -sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" conf/config python -m aurweb.initdb 2>/dev/null || /bin/true diff --git a/docker/php-entrypoint.sh b/docker/php-entrypoint.sh index 8fda1830..5292fa93 100755 --- a/docker/php-entrypoint.sh +++ b/docker/php-entrypoint.sh @@ -1,19 +1,9 @@ #!/bin/bash set -eou pipefail -[[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1 - -DB_NAME="aurweb" -DB_USER="aur" -DB_PASS="aur" - # Setup a config for our mysql db. cp -vf conf/config.dev conf/config sed -i "s;YOUR_AUR_ROOT;$(pwd);g" conf/config -sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" conf/config -sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" conf/config -sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" conf/config -sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" conf/config sed -ri "s;^(aur_location) = .+;\1 = https://localhost:8443;" conf/config diff --git a/docker/test-mysql-entrypoint.sh b/docker/test-mysql-entrypoint.sh index 9594318f..7be3626b 100755 --- a/docker/test-mysql-entrypoint.sh +++ b/docker/test-mysql-entrypoint.sh @@ -1,19 +1,12 @@ #!/bin/bash set -eou pipefail -[[ -z "$DB_HOST" ]] && echo 'Error: $DB_HOST required but missing.' && exit 1 - DB_NAME="aurweb_test" -DB_USER="aur" -DB_PASS="aur" # Setup a config for our mysql db. cp -vf conf/config.dev conf/config sed -i "s;YOUR_AUR_ROOT;$(pwd);g" conf/config sed -ri "s/^(name) = .+/\1 = ${DB_NAME}/" conf/config -sed -ri "s/^(host) = .+/\1 = ${DB_HOST}/" conf/config -sed -ri "s/^(user) = .+/\1 = ${DB_USER}/" conf/config -sed -ri "s/^;?(password) = .+/\1 = ${DB_PASS}/" conf/config # The port can be excluded from use if properly using # volumes to share the mysql socket from the mariadb service. From 1cb1ce0d9947b3459024fcb31115080810b01d25 Mon Sep 17 00:00:00 2001 From: Steven Guikal Date: Tue, 19 Oct 2021 15:23:00 -0400 Subject: [PATCH 2/3] feat(docker): allow production docker setup to use dedicated mariadb Signed-off-by: Steven Guikal --- .env | 1 + docker-compose.override.yml | 23 +++++++++++++++++++++++ docker-compose.prod.yml | 9 ++++++++- docker-compose.yml | 21 --------------------- 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/.env b/.env index 630e856d..92b6bbd2 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ FASTAPI_BACKEND="uvicorn" FASTAPI_WORKERS=2 +MARIADB_SOCKET_DIR="/var/run/mysqld/" diff --git a/docker-compose.override.yml b/docker-compose.override.yml index c0bee88c..26718ac6 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -1,4 +1,24 @@ services: + mariadb: + image: aurweb:latest + init: true + entrypoint: /docker/mariadb-entrypoint.sh + command: /usr/bin/mysqld_safe --datadir=/var/lib/mysql + ports: + # This will expose mariadbd on 127.0.0.1:13306 in the host. + # Ex: `mysql -uaur -paur -h 127.0.0.1 -P 13306 aurweb` + - "13306:3306" + volumes: + - mariadb_run:/var/run/mysqld # Bind socket in this volume. + - mariadb_data:/var/lib/mysql + healthcheck: + test: "bash /docker/health/mariadb.sh" + + mariadb_init: + depends_on: + mariadb: + condition: service_healthy + ca: volumes: - ./cache:/cache @@ -13,6 +33,9 @@ services: - git_data:/aurweb/aur.git - ./cache:/cache - smartgit_run:/var/run/smartgit + depends_on: + mariadb: + condition: service_healthy php-fpm: volumes: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index eb75343b..c63d63f5 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -9,6 +9,7 @@ services: volumes: - git_data:/aurweb/aur.git - cache:/cache + - ${MARIADB_SOCKET_DIR}:/var/run/mysqld smartgit: volumes: @@ -19,12 +20,18 @@ services: php-fpm: volumes: - cache:/cache + - ${MARIADB_SOCKET_DIR}:/var/run/mysqld fastapi: environment: - FASTAPI_BACKEND="gunicorn" volumes: - cache:/cache + - ${MARIADB_SOCKET_DIR}:/var/run/mysqld + + mariadb_init: + volumes: + - ${MARIADB_SOCKET_DIR}:/var/run/mysqld nginx: volumes: @@ -34,7 +41,7 @@ services: - smartgit_run:/var/run/smartgit volumes: - mariadb_run: {} # Share /var/run/mysqld/mysqld.sock + mariadb_run: {} # Share /var/run/mysqld mariadb_data: {} # Share /var/lib/mysql git_data: {} # Share aurweb/aur.git smartgit_run: {} diff --git a/docker-compose.yml b/docker-compose.yml index 55c3527d..3d1da263 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,29 +48,11 @@ services: ports: - "16379:6379" - mariadb: - image: aurweb:latest - init: true - entrypoint: /docker/mariadb-entrypoint.sh - command: /usr/bin/mysqld_safe --datadir=/var/lib/mysql - ports: - # This will expose mariadbd on 127.0.0.1:13306 in the host. - # Ex: `mysql -uaur -paur -h 127.0.0.1 -P 13306 aurweb` - - "13306:3306" - volumes: - - mariadb_run:/var/run/mysqld # Bind socket in this volume. - - mariadb_data:/var/lib/mysql - healthcheck: - test: "bash /docker/health/mariadb.sh" - mariadb_init: image: aurweb:latest init: true entrypoint: /docker/mariadb-init-entrypoint.sh command: echo "MariaDB tables initialized." - depends_on: - mariadb: - condition: service_healthy volumes: - mariadb_run:/var/run/mysqld @@ -100,9 +82,6 @@ services: command: /docker/scripts/run-smartgit.sh healthcheck: test: "bash /docker/health/smartgit.sh" - depends_on: - mariadb: - condition: service_healthy cgit-php: image: aurweb:latest From 4f505ca6c10bf805f19a2bfecc6b24a7221459ff Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Tue, 19 Oct 2021 14:25:59 -0700 Subject: [PATCH 3/3] feat(docker): support for host-mounted mariadb socket A new configurable env var has been introduced to production Docker: MARIADB_SOCKET_DIR, which should contain a path to a directory containing `mysqld.sock` on the Docker host. Note: The database name, user and password can be configured by modifying `conf/config.dev` before building the Docker image. This feature only works in production mode, when specifying: $ export MARIADB_SOCKET_DIR=/var/run/mysqld $ docker-compose -f docker-compose.yml -f docker-compose.prod.yml ... Signed-off-by: Kevin Morris