diff --git a/Dockerfile b/Dockerfile index e6b4e20d..68e19a06 100644 --- a/Dockerfile +++ b/Dockerfile @@ -56,7 +56,12 @@ RUN composer install \ --no-scripts \ --no-autoloader -# Copy app files (npm setup happens at runtime via entrypoint) +# Install npm dependencies (cached layer, only rebuilt when package.json/lock change). +# The postinstall script is skipped here; the assets are copied after the app code below. +COPY package.json package-lock.json ./ +RUN npm ci --omit=dev --ignore-scripts + +# Copy app files COPY . . # Create storage directories @@ -72,6 +77,9 @@ 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 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 db621a44..8ac8640f 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -25,10 +25,14 @@ if [ ! -f vendor/autoload.php ]; then --optimize-autoloader fi -# Install npm dependencies if package.json has dependencies -if grep -q "\"dependencies\"" package.json; then - echo "Installing npm packages..." - npm ci --omit=dev +# npm packages and public/assets/vendor are also built into the image. Only +# (re)create them here as a fallback, e.g. when they are hidden by a volume. +if [ ! -d node_modules ]; then + echo "node_modules/ missing, installing npm packages..." + npm ci --omit=dev --ignore-scripts + node scripts/setup-vendor.js +elif [ -z "$(ls -A public/assets/vendor 2>/dev/null)" ]; then + echo "public/assets/vendor is empty, copying npm packages..." node scripts/setup-vendor.js fi