diff --git a/README.md b/README.md index 9a9629d..f24f454 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ Then visit https://localhost (accept the SSL warning) to complete setup. - `domain` – Primary domain for configuration **Optional:** -- `environment` – Set to `DEV` to start memcached and MySQL locally for development +- `environment` – Set to `DEV` to start memcached and MariaDB locally for development (see "DEV mode" below) - `serveralias` – Comma-separated list of alternative hostnames - `PHPVER` – Informational only. The PHP version is the image tag; in DEV mode a mismatch prints a warning @@ -132,8 +132,8 @@ Then visit https://localhost (accept the SSL warning) to complete setup. --- ## Troubleshooting -- The first run may take several minutes as dependencies are installed. -- If you need to change PHP version, stop and remove the container, then recreate with the desired version. +- The first run may take several minutes as dependencies are installed (DEV mode needs internet access). If it fails you will see `FATAL: DEV mode could not set up ...` in `docker logs` and the container exits. +- If you need to change PHP version, stop and remove the container, then recreate it from the other image tag (`cac:php85`, ...); `PHPVER` does not switch versions. - For advanced configuration, see the scripts in the `scripts/` directory. -- The image is optimized for size and speed, but local development in DEV mode may install additional packages (MariaDB, memcached) at runtime using microdnf. +- The image is optimized for size and speed, but local development in DEV mode installs additional packages (MariaDB, memcached, optionally Xdebug) at runtime using dnf. - The build context is minimized by the included `.dockerignore` file. \ No newline at end of file diff --git a/scripts/dev-mode.sh b/scripts/dev-mode.sh index 7f22417..9a85105 100644 --- a/scripts/dev-mode.sh +++ b/scripts/dev-mode.sh @@ -17,8 +17,33 @@ ## module stream (php:remi-X.Y) the image's PHP was installed from, so the ## extension always matches the PHP build. +## dnf install that tolerates an unreachable repo (mariadb.org down must not +## stop memcached/posix/the AlmaLinux mariadb-server fallback from installing). +dev_dnf() { dnf install -y --setopt=skip_if_unavailable=True "$@"; } + dev_log() { echo "dev-mode: $*"; } +## Failures are collected and reported at the end by dev_report_failures, which +## exits non-zero: a DEV container that cannot give you the database/cache/ +## debugger it promised must not look healthy (the HEALTHCHECK only probes +## Apache, so it would stay green). +DEV_FAILURES=() +dev_fail() { DEV_FAILURES+=("$1"); echo "dev-mode: ERROR: $1" >&2; } + +dev_report_failures() { + [ "${#DEV_FAILURES[@]}" -eq 0 ] && return 0 + { + echo "################################################################" + echo "# FATAL: DEV mode could not set up what it promised:" + for f in "${DEV_FAILURES[@]}"; do echo "# - $f"; done + echo "# (environment=DEV installs MariaDB, memcached and optional PHP" + echo "# extensions from the network at start; check connectivity/DNS.)" + echo "# Exiting non-zero instead of reporting a healthy container." + echo "################################################################" + } >&2 + exit 1 +} + ## PHP version in the image, as "83" for 8.3. The tag selects the version; the ## PHPVER env var does NOT (README). Warn when they disagree so a stale ## -e PHPVER=84 on a cac:php85 container is not mistaken for PHP 8.4. @@ -35,3 +60,71 @@ dev_check_phpver() { echo "################################################################" >&2 fi } + +## Install MariaDB + memcached if absent, start both, create the dev database. +## $1 = customer user. Uses safe-paths.sh helpers (as_user, drop_symlink, +## user_mkdir): the caller has already sourced it. +dev_install_db_and_cache() { + local u=$1 before=${#DEV_FAILURES[@]} + if ! command -v mysqld >/dev/null 2>&1; then + dev_log "installing MariaDB" + if ! dev_dnf --allowerasing MariaDB-server MariaDB-client >/tmp/dev-mariadb-install.log 2>&1; then + dev_log "mariadb.org packages failed (tail of log below); trying AlmaLinux mariadb-server" + tail -n 8 /tmp/dev-mariadb-install.log >&2 + dev_dnf --disablerepo=mariadb mariadb-server >/tmp/dev-mariadb-install2.log 2>&1 \ + || { tail -n 8 /tmp/dev-mariadb-install2.log >&2; dev_fail "MariaDB server could not be installed"; } + fi + fi + if ! command -v memcached >/dev/null 2>&1; then + dev_log "installing memcached" + dev_dnf memcached >/tmp/dev-memcached-install.log 2>&1 \ + || { tail -n 8 /tmp/dev-memcached-install.log >&2; dev_fail "memcached could not be installed"; } + fi + [ "${#DEV_FAILURES[@]}" -eq "$before" ] || return 0 + + sed -r -i 's/session.save_path="memcache:11211/session.save_path="localhost:11211/' /etc/php.ini + # /var/lib/mysql is a docker volume: make sure the mysql user (uid differs by + # package source) owns it, and initialise it if the package did not. + if [ "$(stat -c %U /var/lib/mysql 2>/dev/null)" != "mysql" ]; then chown -R mysql:mysql /var/lib/mysql; fi + if [ ! -d /var/lib/mysql/mysql ]; then + dev_log "initialising empty MariaDB data directory" + mariadb-install-db --user=mysql --datadir=/var/lib/mysql >/tmp/dev-mariadb-initdb.log 2>&1 \ + || { tail -n 8 /tmp/dev-mariadb-initdb.log >&2; dev_fail "mariadb-install-db failed"; return 0; } + fi + # `mysql` is a deprecated alias in MariaDB 11 and prints a warning per call. + local mysql_cli; mysql_cli=$(command -v mariadb || command -v mysql) + nohup mysqld --user=mysql >/var/log/mysqld-dev.log 2>&1 & + local i + for i in $(seq 1 60); do + mysqladmin ping --silent >/dev/null 2>&1 && break + sleep 1 + done + if ! mysqladmin ping --silent >/dev/null 2>&1; then + tail -n 15 /var/log/mysqld-dev.log >&2 + dev_fail "MariaDB did not start within 60 s (log: /var/log/mysqld-dev.log)" + return 0 + fi + dev_log "MariaDB is up" + + if [ ! -f "/home/$u/mysql_creds" ]; then + local mysql_user mysql_password mysql_db + mysql_user=$(tr -dc A-Za-z0-9 /dev/null + printf '%s\n' "MySQL User: $mysql_user" "MySQL Password: $mysql_password" "MySQL Database: devdb_${mysql_db}" \ + | as_user tee "/home/$u/mysql_creds" + fi + /usr/bin/memcached -d -u "$u" || dev_fail "memcached failed to start" +} diff --git a/scripts/entrypoint-fpm.sh b/scripts/entrypoint-fpm.sh index 6632b7d..967c285 100755 --- a/scripts/entrypoint-fpm.sh +++ b/scripts/entrypoint-fpm.sh @@ -64,31 +64,10 @@ open_dir_traversal if [[ $environment == 'DEV' ]]; then echo "Starting Dev Deployment (FPM-only mode)" user_mkdir /home/$user/_db_backups - if ! command -v microdnf &> /dev/null; then - echo "microdnf not found, installing with dnf..." - dnf install -y microdnf && dnf clean all - fi - microdnf install -y MariaDB-server MariaDB-client memcached - sed -r -i 's/session.save_path="memcache:11211/session.save_path="localhost:11211/' /etc/php.ini - nohup mysqld -umysql & - if [ ! -f /home/$user/mysql_creds ]; then - echo "Give MySQL a chance to finish starting..." - sleep 10 - mysql_user=$(tr -dc A-Za-z0-9 /dev/null - printf '%s\n' "MySQL User: $mysql_user" "MySQL Password: $mysql_password" "MySQL Database: devdb_$mysql_db" \ - | as_user tee /home/$user/mysql_creds - fi - /usr/bin/memcached -d -u $user + dev_install_db_and_cache "$user" + # A DEV container that could not set up its database/cache/debugger must not + # look healthy: log loudly and exit non-zero. + dev_report_failures fi if [[ $environment == 'PROD' ]]; then diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index cfb9a17..c560143 100644 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -69,34 +69,10 @@ open_dir_traversal if [[ $environment == 'DEV' ]]; then echo "Starting Dev Deployment" user_mkdir /home/$user/_db_backups - # Ensure microdnf is available for installing MariaDB and memcached in DEV mode - if ! command -v microdnf &> /dev/null; then - echo "microdnf not found, installing with dnf..." - dnf install -y microdnf && dnf clean all - fi - microdnf install -y MariaDB-server MariaDB-client memcached - sed -r -i 's/session.save_path="memcache:11211/session.save_path="localhost:11211/' /etc/php.ini - nohup mysqld -umysql & - if [ ! -f /home/$user/mysql_creds ]; then - echo "Give MySQL a chance to finish starting..." - sleep 10 - mysql_user=$(tr -dc A-Za-z0-9 /dev/null - printf '%s\n' "MySQL User: $mysql_user" "MySQL Password: $mysql_password" "MySQL Database: devdb_$mysql_db" \ - | as_user tee /home/$user/mysql_creds - - fi - /usr/bin/memcached -d -u $user + dev_install_db_and_cache "$user" + # A DEV container that could not set up its database/cache/debugger must not + # look healthy (HEALTHCHECK only probes Apache): log loudly and exit non-zero. + dev_report_failures fi