DEV mode: fix MariaDB/memcached install on AlmaLinux 10 and fail visibly

microdnf install MariaDB-server MariaDB-client memcached died in depsolve (MariaDB-client obsoletes the image's AppStream mariadb; mariadb-client-utils requires it), after which the entrypoint carried on: no mysqld, a mysql_creds file for a database that did not exist, and a container that reported healthy. Now (scripts/dev-mode.sh, used by cac and cac-fpm):
- dnf install --allowerasing MariaDB-server MariaDB-client from mariadb.org 11.4, falling back to AlmaLinux mariadb-server; memcached from AppStream; --setopt=skip_if_unavailable so one dead repo does not block the rest; install skipped when already present (docker start)
- initialise/chown the data dir, start mysqld, wait for mysqladmin ping (60 s) instead of sleep 10
- any failure is logged loudly (FATAL banner) and the entrypoint exits non-zero. The HEALTHCHECK only probes Apache, so 'unhealthy' could not express this; a non-zero exit is unmistakable in docker ps/logs. PROD never runs this code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-23 06:03:24 -08:00
1 parent 7d6a007ef9
commit 0eea05815a
4 files changed
+105 -57

No files matched your search

+93
View File
@@ -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/urandom | head -c 13 ; echo '')
mysql_password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 18 ; echo '')
mysql_db=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 6 ; echo '')
if ! { "$mysql_cli" -e "CREATE DATABASE devdb_${mysql_db};" \
&& "$mysql_cli" -e "CREATE USER '${mysql_user}'@'localhost' IDENTIFIED BY '${mysql_password}';" \
&& "$mysql_cli" -e "GRANT ALL PRIVILEGES ON *.* TO '${mysql_user}'@'localhost' WITH GRANT OPTION;" \
&& "$mysql_cli" -e "FLUSH PRIVILEGES;"; }; then
dev_fail "could not create the dev database/user"
return 0
fi
# Create user crontab with MySQL backup job. Written AS THE CUSTOMER
# (as_user tee), never by root through a customer path.
drop_symlink "/home/$u/crontab"
printf '%s\n' "# User crontab for $u" "*/15 * * * * /scripts/mysql-backup.sh $u devdb_${mysql_db}" \
| as_user tee "/home/$u/crontab" >/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"
}
+4 -25
View File
@@ -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/urandom | head -c 13 ; echo '')
mysql_password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 18 ; echo '')
mysql_db=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 6 ; echo '')
mysql -e "CREATE DATABASE devdb_"$mysql_db";"
mysql -e "CREATE USER '"$mysql_user"'@'localhost' IDENTIFIED BY '"$mysql_password"';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO '"$mysql_user"'@'localhost' WITH GRANT OPTION;"
mysql -e "FLUSH PRIVILEGES;"
# Written AS THE CUSTOMER (as_user tee), never by root through a customer path.
drop_symlink /home/$user/crontab
printf '%s\n' "# User crontab for $user" "*/15 * * * * /scripts/mysql-backup.sh $user devdb_$mysql_db" \
| as_user tee /home/$user/crontab >/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
+4 -28
View File
@@ -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/urandom | head -c 13 ; echo '')
mysql_password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 18 ; echo '')
mysql_db=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 6 ; echo '')
mysql -e "CREATE DATABASE devdb_"$mysql_db";"
mysql -e "CREATE USER '"$mysql_user"'@'localhost' IDENTIFIED BY '"$mysql_password"';"
mysql -e "GRANT ALL PRIVILEGES ON *.* TO '"$mysql_user"'@'localhost' WITH GRANT OPTION;"
mysql -e "FLUSH PRIVILEGES;"
# Create user crontab with MySQL backup job. Written AS THE CUSTOMER
# (as_user tee), never by root through a customer path.
drop_symlink /home/$user/crontab
printf '%s\n' "# User crontab for $user" "*/15 * * * * /scripts/mysql-backup.sh $user devdb_$mysql_db" \
| as_user tee /home/$user/crontab >/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