fix(docker): build Vite assets at boot instead of syncing from image

This commit is contained in:
Thorsten Bus 2026-03-31 11:28:43 +02:00
parent 1fb07f40c9
commit 00bffab74f
2 changed files with 9 additions and 23 deletions

View file

@ -50,22 +50,15 @@ COPY . .
# Run composer post-autoload-dump scripts (package discovery etc.) # Run composer post-autoload-dump scripts (package discovery etc.)
RUN composer run-script post-autoload-dump --no-interaction || true RUN composer run-script post-autoload-dump --no-interaction || true
# Build Vite assets (outputs to public/build/)
RUN npm run build RUN npm run build
# Copy public/ → public-build/ so the production stage retains built assets
# even after public/ becomes a runtime bind-mount target.
RUN cp -r public/ public-build/
# ============================================================================= # =============================================================================
# Stage 2: Production # Stage 2: Production
# PHP-FPM runtime image with all system dependencies. # PHP-FPM runtime image with all system dependencies.
# Node.js is NOT present here.
# ============================================================================= # =============================================================================
FROM php:8.4-fpm-bookworm AS production FROM php:8.4-fpm-bookworm AS production
# System packages (excluding Node.js — not needed at runtime)
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
supervisor \ supervisor \
libzip-dev \ libzip-dev \
@ -84,6 +77,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
git \ git \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# Node.js 20 LTS — needed at boot to build Vite assets into the bind-mounted public/
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
&& apt-get install -y --no-install-recommends nodejs \
&& rm -rf /var/lib/apt/lists/*
# LibreOffice for PowerPoint → PDF conversion (large layer, separate cache) # LibreOffice for PowerPoint → PDF conversion (large layer, separate cache)
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
libreoffice-impress \ libreoffice-impress \
@ -107,9 +105,6 @@ WORKDIR /app
# ── Copy built application from build stage ────────────────────────────────── # ── Copy built application from build stage ──────────────────────────────────
COPY --from=build /app /app COPY --from=build /app /app
# Remove node_modules — not needed at runtime
RUN rm -rf /app/node_modules
# ── Install Docker config files ────────────────────────────────────────────── # ── Install Docker config files ──────────────────────────────────────────────
# Supervisord (manages php-fpm, queue:work, schedule:work) # Supervisord (manages php-fpm, queue:work, schedule:work)
COPY build/supervisord.conf /etc/supervisor/conf.d/supervisord.conf COPY build/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
@ -138,8 +133,7 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
cgi-fcgi -bind -connect 127.0.0.1:9000 2>/dev/null | grep -q "pong" || exit 1 cgi-fcgi -bind -connect 127.0.0.1:9000 2>/dev/null | grep -q "pong" || exit 1
# boot-container.sh runs as root: creates dirs, sets permissions, # boot-container.sh runs as root: creates dirs, sets permissions,
# creates DB on first run, runs migrations, runs cache commands, # creates DB on first run, builds Vite assets, runs migrations,
# syncs built assets to the bind-mounted public/ directory, # warms caches, then exec's supervisord (CMD).
# then exec's supervisord (CMD).
ENTRYPOINT ["/app/build/boot-container.sh"] ENTRYPOINT ["/app/build/boot-container.sh"]
CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"] CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

View file

@ -24,18 +24,10 @@ mkdir -p \
chown -R www-data:www-data storage bootstrap/cache database 2>/dev/null || true chown -R www-data:www-data storage bootstrap/cache database 2>/dev/null || true
chmod -R 775 storage bootstrap/cache database 2>/dev/null || true chmod -R 775 storage bootstrap/cache database 2>/dev/null || true
# Remove Vite dev server hot file if it leaked into the bind-mounted public/
# (causes Laravel to try localhost:5173 instead of reading the manifest)
rm -f /app/public/hot rm -f /app/public/hot
# Sync built Vite assets from the image (public-build/) into the echo "[boot] Building Vite assets..."
# bind-mounted public/ directory. This ensures Caddy always serves npm run build
# up-to-date assets even though public/ is a host bind-mount.
if [ -d /app/public-build ]; then
echo "[boot] Syncing built assets to public/..."
cp -r /app/public-build/. /app/public/
chown -R www-data:www-data /app/public 2>/dev/null || true
fi
# Create the storage symlink after volumes are mounted # Create the storage symlink after volumes are mounted
# (public/storage → storage/app/public) # (public/storage → storage/app/public)