Skip to content

Commit e919fe0

Browse files
Corentin Joubertclaude
andcommitted
fix: make image rootless and OpenShift-compatible, harden entrypoint
- Drop named user creation; use numeric UID 1000 with GID 0 ownership and g+rwX on all writable dirs so OpenShift's arbitrary UID can write - Make /etc/passwd group-writable and register runtime UID at startup so WP-CLI / PHP getpwuid() calls resolve correctly - Set HOME=/app for WP-CLI cache with arbitrary UID - Replace grep+sed salt loop with wp config shuffle-salts - Fix wp-config.php permissions 0644 → 0600 - Drop --allow-root from wp() wrapper - Use wp db query "SELECT 1" instead of wp db check in readiness loop - Call configure_wordpress on every start, not only on fresh install - Only print auto-generated password when WORDPRESS_ADMIN_PASSWORD unset - Pin imagick/imagick@3.7.0 (was @master) - Disable opcache timestamp validation (files don't change in container) - Remove dead TMPDIR mktemp / redundant wp-content chown Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2c3f73f commit e919fe0

2 files changed

Lines changed: 27 additions & 24 deletions

File tree

Dockerfile

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
FROM dunglas/frankenphp:1.11-php8.4
22

3-
ARG USER=appuser
43
ARG WORDPRESS_VERSION=6.9
54
COPY entrypoint.sh /docker-entrypoint.sh
65
RUN \
7-
useradd ${USER}; \
8-
setcap -r /usr/local/bin/frankenphp; \
9-
# Give write access to /config/caddy and /data/caddy
10-
chown -R ${USER}:${USER} /config/caddy /data/caddy /app && \
6+
setcap -r /usr/local/bin/frankenphp; \
7+
chown -R 1000:0 /config/caddy /data/caddy /app && \
8+
chmod -R g+rwX /config/caddy /data/caddy /app && \
119
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" && \
12-
chmod 755 /docker-entrypoint.sh
10+
chmod g=u /etc/passwd && \
11+
chmod 755 /docker-entrypoint.sh
1312

1413
RUN install-php-extensions \
1514
bcmath \
@@ -19,7 +18,7 @@ RUN install-php-extensions \
1918
mysqli \
2019
zip \
2120
# See https://github.com/Imagick/imagick/issues/640#issuecomment-2077206945
22-
imagick/imagick@master \
21+
imagick/imagick@3.7.0 \
2322
opcache
2423
RUN set -eux; \
2524
apt-get update; \
@@ -33,8 +32,8 @@ RUN set -eux; \
3332
echo 'opcache.memory_consumption=128'; \
3433
echo 'opcache.interned_strings_buffer=8'; \
3534
echo 'opcache.max_accelerated_files=4000'; \
36-
echo 'opcache.revalidate_freq=2'; \
37-
echo 'opcache.validate_timestamps=1'; \
35+
echo 'opcache.revalidate_freq=0'; \
36+
echo 'opcache.validate_timestamps=0'; \
3837
echo 'opcache.save_comments=1'; \
3938
echo 'opcache.fast_shutdown=1'; \
4039
} > "$PHP_INI_DIR/conf.d/opcache-recommended.ini" && \
@@ -51,17 +50,15 @@ RUN set -eux; \
5150
} > "$PHP_INI_DIR/conf.d/error-logging.ini"
5251

5352
RUN \
54-
TMPDIR=$(mktemp -d) && \
5553
curl -L https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz | \
5654
tar -xzf - -C /app/public --strip-components=1 && \
57-
rm -rf ${TMPDIR} && \
5855
curl -L -o /usr/local/bin/wp https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar && \
5956
chmod 755 /usr/local/bin/wp && \
6057
curl -L -o /app/public/wp-config-docker.php https://raw.githubusercontent.com/docker-library/wordpress/master/wp-config-docker.php && \
61-
chown -R ${USER}:${USER} /app/public && \
62-
mkdir -p /app/public/wp-content && \
63-
chown -R ${USER}:${USER} /app/public/wp-content
58+
chown -R 1000:0 /app/public && \
59+
chmod -R g+rwX /app/public
6460

65-
USER ${USER}
61+
ENV HOME=/app
62+
USER 1000
6663
ENTRYPOINT ["/docker-entrypoint.sh"]
6764
CMD ["frankenphp", "run", "--config", "/etc/caddy/Caddyfile"]

entrypoint.sh

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ WORDPRESS_DIR="/app/public"
55
WP_CLI="/usr/local/bin/wp"
66

77
wp() {
8-
"$WP_CLI" --path="$WORDPRESS_DIR" --allow-root "$@"
8+
"$WP_CLI" --path="$WORDPRESS_DIR" "$@"
99
}
1010

1111
check_wordpress_installation() {
@@ -67,7 +67,9 @@ install_wordpress() {
6767
echo >&2 "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
6868
echo >&2 "URL: $WP_URL"
6969
echo >&2 "Admin: $WP_ADMIN_USER"
70-
echo >&2 "Password: $WP_ADMIN_PASSWORD"
70+
if [ -z "${WORDPRESS_ADMIN_PASSWORD:-}" ]; then
71+
echo >&2 "Password: $WP_ADMIN_PASSWORD"
72+
fi
7173
echo >&2 "Email: $WP_ADMIN_EMAIL"
7274
echo >&2 "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
7375

@@ -93,13 +95,9 @@ generate_wp_config_if_needed() {
9395

9496
cp "$WORDPRESS_DIR/wp-config-docker.php" "$WORDPRESS_DIR/wp-config.php"
9597

96-
# Remplacer chaque occurrence de "put your unique phrase here" par une clé aléatoire (alphanum uniquement)
97-
while grep -q "put your unique phrase here" "$WORDPRESS_DIR/wp-config.php"; do
98-
RANDOM_KEY="$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 64)"
99-
sed -i "0,/put your unique phrase here/s/put your unique phrase here/$RANDOM_KEY/" "$WORDPRESS_DIR/wp-config.php"
100-
done
98+
wp config shuffle-salts >/dev/null 2>&1 || true
10199

102-
chmod 0644 "$WORDPRESS_DIR/wp-config.php"
100+
chmod 0600 "$WORDPRESS_DIR/wp-config.php"
103101
echo >&2 "✓ wp-config.php créé"
104102
fi
105103

@@ -141,7 +139,7 @@ wait_for_db_wpcli() {
141139

142140
# On capture l’erreur WP-CLI pour la loguer
143141
local out
144-
if out="$(wp db check 2>&1)"; then
142+
if out="$(wp db query "SELECT 1" 2>&1)"; then
145143
echo >&2 "✓ Base de données accessible (WP-CLI) après ${elapsed}s (tentatives: ${attempt})"
146144
return 0
147145
fi
@@ -167,6 +165,12 @@ wait_for_db_wpcli() {
167165
}
168166

169167
main() {
168+
# OpenShift assigns an arbitrary UID at runtime; register it in /etc/passwd so
169+
# tools that call getpwuid() (WP-CLI, PHP, openssl) can resolve the username.
170+
if ! whoami &>/dev/null && [ -w /etc/passwd ]; then
171+
echo "appuser:x:$(id -u):0:appuser:/app:/sbin/nologin" >> /etc/passwd
172+
fi
173+
170174
cd "$WORDPRESS_DIR"
171175

172176
# Génération du wp-config.php (si nécessaire)
@@ -193,6 +197,8 @@ main() {
193197
echo >&2 "✗ Installation WordPress échouée → arrêt du conteneur (exit 2)"
194198
exit 2
195199
fi
200+
else
201+
configure_wordpress
196202
fi
197203

198204
# Lancement du process principal (FrankenPHP)

0 commit comments

Comments
 (0)