diff --git a/Dockerfile b/Dockerfile index 102cea5..82aba7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,58 +1,53 @@ # syntax=docker/dockerfile:1.6 -ARG PHP_VERSION=8.3 -ARG NODE_VERSION=20 +################################# +# Stage 1: Build PHP + Composer # +################################# +FROM php:8.3-fpm-alpine AS php_build -############################ -# Stage 1: PHP (Composer) # -############################ -FROM php:${PHP_VERSION}-fpm-alpine AS composer_build - -# OS deps for building PHP extensions that Composer plugins may need +# System deps for PHP extensions RUN set -eux; \ apk add --no-cache git unzip icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base -# PHP extensions required by Laravel (build here too if scripts need them) +# PHP extensions needed by Laravel RUN set -eux; \ docker-php-ext-configure zip; \ docker-php-ext-install -j"$(nproc)" \ - pdo_mysql pdo_pgsql pgsql zip intl mbstring bcmath opcache + pdo_mysql pdo_pgsql pgsql zip intl mbstring bcmath opcache # Composer ENV COMPOSER_ALLOW_SUPERUSER=1 COMPOSER_HOME=/tmp/composer COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer WORKDIR /app -# Install PHP deps first for better layer caching -COPY composer.json composer.lock ./ -# DO NOT skip scripts — Laravel needs them (package discovery, etc.) -RUN composer install \ - --no-dev --no-interaction --prefer-dist \ - --optimize-autoloader -# Bring in the rest of the app +# --- PASS 1: deps only (cacheable), NO scripts (artisan not present yet) --- +COPY composer.json composer.lock ./ +RUN composer install \ + --no-dev --no-interaction --prefer-dist \ + --no-scripts --no-progress --optimize-autoloader + +# Now copy the whole application COPY . . -############################ -# Stage 2: Frontend (Vite) # -############################ -FROM node:${NODE_VERSION}-alpine AS node_build +# --- PASS 2: run scripts now that artisan exists --- +# (This is quick: vendor is already populated; this mainly runs package:discover) +RUN composer install \ + --no-dev --no-interaction --prefer-dist \ + --optimize-autoloader + +################################# +# Stage 2: Build Frontend (Vite)# +################################# +FROM node:20-alpine AS node_build WORKDIR /app -# Bring vendor so Vite can resolve ../../vendor/... imports (e.g., Livewire Flux CSS) -COPY --from=composer_build /app/vendor /app/vendor +# Bring full app INCLUDING vendor so imports like ../../vendor/... work +COPY --from=php_build /app /app -# Only the files Node needs (faster context & cache) -COPY package.json package-lock.json* pnpm-lock.yaml* yarn.lock* vite.config.* ./ -COPY resources ./resources -COPY public ./public -# If your Vite config imports from `resources` only, this is enough. -# If you reference other paths at build time, copy them similarly. - -# Native build deps (rarely needed, but safe) RUN apk add --no-cache python3 make g++ -# Install using the appropriate lockfile +# Install JS deps with the appropriate lockfile RUN set -eux; \ if [ -f pnpm-lock.yaml ]; then corepack enable && pnpm i --frozen-lockfile; \ elif [ -f yarn.lock ]; then corepack enable && yarn install --frozen-lockfile; \ @@ -60,28 +55,26 @@ RUN set -eux; \ else npm i; fi ENV NODE_ENV=production -# Standard Laravel vite build => outputs to public/build + manifest.json RUN npm run build -##################################### -# Stage 3: Runtime (nginx + php-fpm)# -##################################### -FROM php:${PHP_VERSION}-fpm-alpine +################################# +# Stage 3: Runtime Image # +################################# +FROM php:8.3-fpm-alpine -# Base runtime packages +# Runtime packages RUN set -eux; \ - apk add --no-cache nginx supervisor curl libzip icu-libs oniguruma libpq + apk add --no-cache nginx supervisor curl icu-libs libzip oniguruma libpq -# Build required PHP extensions in the final image (so they exist at runtime) +# PHP runtime extensions RUN set -eux; \ - apk add --no-cache libzip-dev icu-dev oniguruma-dev postgresql-dev autoconf build-base; \ + apk add --no-cache icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base; \ docker-php-ext-configure zip; \ docker-php-ext-install -j"$(nproc)" \ - pdo_mysql pdo_pgsql pgsql zip intl mbstring bcmath opcache; \ - # Slim back down - apk del --no-progress --purge libzip-dev icu-dev oniguruma-dev postgresql-dev autoconf build-base || true + pdo_mysql pdo_pgsql pgsql zip intl mbstring bcmath opcache; \ + apk del --no-progress --purge icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base || true -# php.ini production + opcache tuning +# php.ini production + a bit of opcache tuning RUN set -eux; \ mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"; \ { \ @@ -94,7 +87,7 @@ RUN set -eux; \ echo "realpath_cache_ttl=600"; \ } >> "$PHP_INI_DIR/conf.d/99-opcache.ini" -# Configure PHP-FPM: pass env, listen TCP, run as nginx +# FPM config (listen on TCP for nginx) RUN set -eux; \ sed -ri 's|^;?clear_env\s*=.*|clear_env = no|g' /usr/local/etc/php-fpm.d/www.conf; \ sed -ri 's|^listen = .*|listen = 127.0.0.1:9000|g' /usr/local/etc/php-fpm.d/www.conf; \ @@ -102,7 +95,7 @@ RUN set -eux; \ sed -ri 's|^group\s*=.*|group = nginx|g' /usr/local/etc/php-fpm.d/www.conf; \ { echo "ping.path = /ping"; echo "pm.status_path = /status"; echo "catch_workers_output = yes"; } >> /usr/local/etc/php-fpm.d/www.conf -# Runtime dirs +# Paths RUN set -eux; \ mkdir -p /run/nginx /var/log/nginx /var/log/supervisor \ /etc/nginx/conf.d /var/www/html \ @@ -110,40 +103,28 @@ RUN set -eux; \ WORKDIR /var/www/html -# Copy application code (without node_modules) and vendor from composer stage -COPY --from=composer_build --chown=nginx:nginx /app /var/www/html - -# Copy built frontend assets (public/build with manifest.json) +# App + built assets +COPY --from=php_build --chown=nginx:nginx /app /var/www/html COPY --from=node_build --chown=nginx:nginx /app/public/build /var/www/html/public/build -# Nginx + Supervisor configs + health + entrypoint (these must exist in your repo) -COPY ./docker/nginx.conf /etc/nginx/nginx.conf -COPY ./docker/site.conf /etc/nginx/conf.d/default.conf -COPY ./docker/supervisord.conf /etc/supervisord.conf -COPY ./docker/healthcheck.sh /usr/local/bin/healthcheck.sh -COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh -RUN chmod +x /usr/local/bin/healthcheck.sh /usr/local/bin/entrypoint.sh +# Configs (use the nginx site config you posted) +COPY ./docker/nginx.conf /etc/nginx/nginx.conf +COPY ./docker/site.conf /etc/nginx/conf.d/default.conf +COPY ./docker/supervisord.conf /etc/supervisord.conf +COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh -# Log to STDOUT/ERR -RUN set -eux; \ - ln -sf /dev/stdout /var/log/nginx/access.log; \ +# Logs to STDOUT/ERR +RUN ln -sf /dev/stdout /var/log/nginx/access.log && \ ln -sf /dev/stderr /var/log/nginx/error.log -# Laravel writable dirs & permissions -RUN set -eux; \ - mkdir -p storage/framework/{cache,sessions,views} storage/logs bootstrap/cache; \ - chown -R nginx:nginx storage bootstrap/cache \ - /var/cache/nginx /var/lib/nginx /run/nginx /var/log/nginx /var/log/supervisor; \ +# Laravel writable dirs & perms +RUN mkdir -p storage/framework/{cache,sessions,views} storage/logs bootstrap/cache && \ + chown -R nginx:nginx storage bootstrap/cache && \ chmod -R ug+rwX storage bootstrap/cache -# Environment + port (ensure site.conf listens on 8080 or change EXPOSE to 80) ENV APP_ENV=production APP_DEBUG=false APP_URL=http://localhost APP_PORT=8080 EXPOSE 8080 -HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD ["/usr/local/bin/healthcheck.sh"] - -# Drop root USER nginx:nginx - -# Start services CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"] diff --git a/docker/site.conf b/docker/site.conf index 3c7d87b..55d893c 100644 --- a/docker/site.conf +++ b/docker/site.conf @@ -5,10 +5,13 @@ server { root /var/www/html/public; index index.php index.html; - # Readiness/Liveness - location = /healthz { access_log off; return 200 "ok\n"; } + # Health checks + location = /healthz { + access_log off; + return 200 "ok\n"; + } - # FPM Ping/Status (optional absichern) + # FPM ping/status (optional, secure in prod) location = /fpm-ping { access_log off; include fastcgi_params; @@ -17,6 +20,7 @@ server { fastcgi_param SCRIPT_NAME /ping; fastcgi_param PATH_INFO /ping; } + location = /fpm-status { access_log off; include fastcgi_params; @@ -26,10 +30,12 @@ server { fastcgi_param PATH_INFO /status; } + # Main app location / { try_files $uri $uri/ /index.php?$query_string; } + # PHP handling location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; @@ -39,9 +45,12 @@ server { fastcgi_buffers 16 16k; } - location ~ /\.ht { deny all; } + # Deny hidden files + location ~ /\.ht { + deny all; + } - # optionale Asset-Caches + # Cache static assets location ~* \.(?:css|js|jpg|jpeg|gif|png|svg|ico|webp|woff2?)$ { access_log off; expires 7d;