From 2ddcb1b6b00cbcf6e2b8b36ee325f121ae99c5b3 Mon Sep 17 00:00:00 2001 From: Eric Bus Date: Fri, 20 Nov 2026 13:33:07 +0100 Subject: [PATCH] docker: run composer install at build time instead of in the entrypoint - Install dependencies in a cached layer (composer.json/lock only), then dump the optimized autoloader after copying the app - Entrypoint only runs composer install if vendor/ is missing (e.g. when the app directory is mounted as a volume) - Force LF for *.sh so the entrypoint works when built from a Windows checkout Co-Authored-By: Claude Opus 5.5 --- .gitattributes | 1 + Dockerfile | 16 ++++++++++++---- docker-entrypoint.sh | 17 ++++++++++------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/.gitattributes b/.gitattributes index 967315dd..6cf59a0a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,3 +3,4 @@ *.scss linguist-vendored *.js linguist-vendored CHANGELOG.md export-ignore +*.sh text eol=lf diff --git a/Dockerfile b/Dockerfile index f79b4f7f..e6b4e20d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -46,12 +46,17 @@ RUN echo '\n\ ' > /etc/apache2/conf-available/laravel.conf \ && a2enconf laravel -# Copy app files (composer/npm setup happens at runtime via entrypoint) +# Install PHP dependencies first (cached layer, only rebuilt when composer files change). +# Scripts and autoloader are skipped here because the app code isn't copied yet. COPY composer.json composer.lock ./ -COPY package.json package-lock.json* ./ -COPY scripts/ ./scripts/ 2>/dev/null +RUN composer install \ + --no-dev \ + --prefer-dist \ + --no-interaction \ + --no-scripts \ + --no-autoloader -# Note: for Laravel, the COPY . . needs to be before the composer install +# Copy app files (npm setup happens at runtime via entrypoint) COPY . . # Create storage directories @@ -64,6 +69,9 @@ RUN mkdir -p \ bootstrap/cache \ public/assets/vendor +# Generate the optimized autoloader and run composer scripts (package:discover) +RUN composer dump-autoload --no-dev --optimize --no-interaction + # Copy and set up entrypoint script COPY docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 29955b6e..db621a44 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -14,13 +14,16 @@ 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 -# Install PHP dependencies -echo "Installing PHP packages..." -composer install \ - --no-dev \ - --prefer-dist \ - --no-interaction \ - --optimize-autoloader +# 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/. +if [ ! -f vendor/autoload.php ]; then + echo "vendor/ missing, installing PHP packages..." + composer install \ + --no-dev \ + --prefer-dist \ + --no-interaction \ + --optimize-autoloader +fi # Install npm dependencies if package.json has dependencies if grep -q "\"dependencies\"" package.json; then