From da971e164691d187dfffa12ffd5f92d948c25812 Mon Sep 17 00:00:00 2001 From: Eric Bus Date: Fri, 20 Nov 2026 14:17:42 +0100 Subject: [PATCH] docker: support running the container as www-data (uid 33) - Set ownership of storage, bootstrap/cache and public/assets/vendor at build time - Entrypoint only creates/chowns directories when running as root Co-Authored-By: Claude Opus 5.5 --- Dockerfile | 7 +++++-- docker-entrypoint.sh | 30 ++++++++++++++++-------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index 68e19a06..df18001a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -77,8 +77,11 @@ RUN mkdir -p \ # Generate the optimized autoloader and run composer scripts (package:discover) RUN composer dump-autoload --no-dev --optimize --no-interaction -# Copy the npm packages into public/assets/vendor -RUN node scripts/setup-vendor.js +# Copy the npm packages into public/assets/vendor, then make the directories +# Laravel writes to owned by www-data, so the container can run as uid 33 +RUN node scripts/setup-vendor.js \ + && chown -R www-data:www-data storage bootstrap/cache public/assets/vendor \ + && chmod -R ug+rwX storage bootstrap/cache public/assets/vendor # Copy and set up entrypoint script COPY docker-entrypoint.sh /usr/local/bin/ diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 8ac8640f..59a293c6 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -6,13 +6,20 @@ cd /var/www/app # Set HOME for npm cache (www-data user needs writable cache directory) export HOME=/tmp -echo "Setting up dependencies..." +WRITABLE_DIRS="storage/framework storage/logs bootstrap/cache public/assets/vendor" -# Create vendor directories and fix permissions -echo "Fixing permissions..." -mkdir -p bootstrap/cache storage/framework storage/logs public/assets/vendor -chown -R www-data:www-data bootstrap/cache storage/framework storage/logs public/assets/vendor -chmod -R ug+rwX bootstrap/cache storage/framework storage/logs public/assets/vendor +# Ownership is already set in the image. Only fix it when running as root, +# e.g. when a root-owned volume is mounted over one of these directories. +# When running as www-data (uid 33) we can't (and don't need to) chown. +fix_permissions() { + if [ "$(id -u)" = "0" ]; then + mkdir -p $WRITABLE_DIRS + chown -R www-data:www-data $WRITABLE_DIRS + chmod -R ug+rwX $WRITABLE_DIRS + fi +} + +fix_permissions # PHP dependencies are installed in the image. Only install them here as a # fallback, e.g. when the app directory is mounted as a volume without vendor/. @@ -36,13 +43,8 @@ elif [ -z "$(ls -A public/assets/vendor 2>/dev/null)" ]; then node scripts/setup-vendor.js fi -# Fix permissions -echo "Fixing permissions..." -chown -R www-data:www-data \ - bootstrap/cache \ - public/assets/vendor +# Files created by the fallbacks above may be owned by root +fix_permissions -chmod -R ug+rwX bootstrap/cache - -echo "✓ Setup complete, starting Apache..." +echo "Starting Apache..." exec apache2-foreground