This commit is contained in:
u00lipp
2025-10-22 23:26:58 +02:00
parent 462f6753d3
commit 056dcbfc52
2 changed files with 67 additions and 77 deletions
+53 -72
View File
@@ -1,58 +1,53 @@
# syntax=docker/dockerfile:1.6 # 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
############################ # System deps for PHP extensions
# Stage 1: PHP (Composer) #
############################
FROM php:${PHP_VERSION}-fpm-alpine AS composer_build
# OS deps for building PHP extensions that Composer plugins may need
RUN set -eux; \ RUN set -eux; \
apk add --no-cache git unzip icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base 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; \ RUN set -eux; \
docker-php-ext-configure zip; \ docker-php-ext-configure zip; \
docker-php-ext-install -j"$(nproc)" \ 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 # Composer
ENV COMPOSER_ALLOW_SUPERUSER=1 COMPOSER_HOME=/tmp/composer ENV COMPOSER_ALLOW_SUPERUSER=1 COMPOSER_HOME=/tmp/composer
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
WORKDIR /app 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 . . COPY . .
############################ # --- PASS 2: run scripts now that artisan exists ---
# Stage 2: Frontend (Vite) # # (This is quick: vendor is already populated; this mainly runs package:discover)
############################ RUN composer install \
FROM node:${NODE_VERSION}-alpine AS node_build --no-dev --no-interaction --prefer-dist \
--optimize-autoloader
#################################
# Stage 2: Build Frontend (Vite)#
#################################
FROM node:20-alpine AS node_build
WORKDIR /app WORKDIR /app
# Bring vendor so Vite can resolve ../../vendor/... imports (e.g., Livewire Flux CSS) # Bring full app INCLUDING vendor so imports like ../../vendor/... work
COPY --from=composer_build /app/vendor /app/vendor 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++ RUN apk add --no-cache python3 make g++
# Install using the appropriate lockfile # Install JS deps with the appropriate lockfile
RUN set -eux; \ RUN set -eux; \
if [ -f pnpm-lock.yaml ]; then corepack enable && pnpm i --frozen-lockfile; \ if [ -f pnpm-lock.yaml ]; then corepack enable && pnpm i --frozen-lockfile; \
elif [ -f yarn.lock ]; then corepack enable && yarn install --frozen-lockfile; \ elif [ -f yarn.lock ]; then corepack enable && yarn install --frozen-lockfile; \
@@ -60,28 +55,26 @@ RUN set -eux; \
else npm i; fi else npm i; fi
ENV NODE_ENV=production ENV NODE_ENV=production
# Standard Laravel vite build => outputs to public/build + manifest.json
RUN npm run build RUN npm run build
##################################### #################################
# Stage 3: Runtime (nginx + php-fpm)# # Stage 3: Runtime Image #
##################################### #################################
FROM php:${PHP_VERSION}-fpm-alpine FROM php:8.3-fpm-alpine
# Base runtime packages # Runtime packages
RUN set -eux; \ 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; \ 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-configure zip; \
docker-php-ext-install -j"$(nproc)" \ 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; \
# Slim back down apk del --no-progress --purge icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base || true
apk del --no-progress --purge libzip-dev icu-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; \ RUN set -eux; \
mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"; \ mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"; \
{ \ { \
@@ -94,7 +87,7 @@ RUN set -eux; \
echo "realpath_cache_ttl=600"; \ echo "realpath_cache_ttl=600"; \
} >> "$PHP_INI_DIR/conf.d/99-opcache.ini" } >> "$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; \ RUN set -eux; \
sed -ri 's|^;?clear_env\s*=.*|clear_env = no|g' /usr/local/etc/php-fpm.d/www.conf; \ 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; \ 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; \ 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 { 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; \ RUN set -eux; \
mkdir -p /run/nginx /var/log/nginx /var/log/supervisor \ mkdir -p /run/nginx /var/log/nginx /var/log/supervisor \
/etc/nginx/conf.d /var/www/html \ /etc/nginx/conf.d /var/www/html \
@@ -110,40 +103,28 @@ RUN set -eux; \
WORKDIR /var/www/html WORKDIR /var/www/html
# Copy application code (without node_modules) and vendor from composer stage # App + built assets
COPY --from=composer_build --chown=nginx:nginx /app /var/www/html COPY --from=php_build --chown=nginx:nginx /app /var/www/html
# Copy built frontend assets (public/build with manifest.json)
COPY --from=node_build --chown=nginx:nginx /app/public/build /var/www/html/public/build 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) # Configs (use the nginx site config you posted)
COPY ./docker/nginx.conf /etc/nginx/nginx.conf COPY ./docker/nginx.conf /etc/nginx/nginx.conf
COPY ./docker/site.conf /etc/nginx/conf.d/default.conf COPY ./docker/site.conf /etc/nginx/conf.d/default.conf
COPY ./docker/supervisord.conf /etc/supervisord.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
COPY ./docker/entrypoint.sh /usr/local/bin/entrypoint.sh RUN chmod +x /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/healthcheck.sh /usr/local/bin/entrypoint.sh
# Log to STDOUT/ERR # Logs to STDOUT/ERR
RUN set -eux; \ RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
ln -sf /dev/stdout /var/log/nginx/access.log; \
ln -sf /dev/stderr /var/log/nginx/error.log ln -sf /dev/stderr /var/log/nginx/error.log
# Laravel writable dirs & permissions # Laravel writable dirs & perms
RUN set -eux; \ RUN mkdir -p storage/framework/{cache,sessions,views} storage/logs bootstrap/cache && \
mkdir -p storage/framework/{cache,sessions,views} storage/logs bootstrap/cache; \ chown -R nginx:nginx storage bootstrap/cache && \
chown -R nginx:nginx storage bootstrap/cache \
/var/cache/nginx /var/lib/nginx /run/nginx /var/log/nginx /var/log/supervisor; \
chmod -R ug+rwX 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 ENV APP_ENV=production APP_DEBUG=false APP_URL=http://localhost APP_PORT=8080
EXPOSE 8080 EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD ["/usr/local/bin/healthcheck.sh"]
# Drop root
USER nginx:nginx USER nginx:nginx
# Start services
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"] CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
+14 -5
View File
@@ -5,10 +5,13 @@ server {
root /var/www/html/public; root /var/www/html/public;
index index.php index.html; index index.php index.html;
# Readiness/Liveness # Health checks
location = /healthz { access_log off; return 200 "ok\n"; } location = /healthz {
access_log off;
return 200 "ok\n";
}
# FPM Ping/Status (optional absichern) # FPM ping/status (optional, secure in prod)
location = /fpm-ping { location = /fpm-ping {
access_log off; access_log off;
include fastcgi_params; include fastcgi_params;
@@ -17,6 +20,7 @@ server {
fastcgi_param SCRIPT_NAME /ping; fastcgi_param SCRIPT_NAME /ping;
fastcgi_param PATH_INFO /ping; fastcgi_param PATH_INFO /ping;
} }
location = /fpm-status { location = /fpm-status {
access_log off; access_log off;
include fastcgi_params; include fastcgi_params;
@@ -26,10 +30,12 @@ server {
fastcgi_param PATH_INFO /status; fastcgi_param PATH_INFO /status;
} }
# Main app
location / { location / {
try_files $uri $uri/ /index.php?$query_string; try_files $uri $uri/ /index.php?$query_string;
} }
# PHP handling
location ~ \.php$ { location ~ \.php$ {
include fastcgi_params; include fastcgi_params;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
@@ -39,9 +45,12 @@ server {
fastcgi_buffers 16 16k; 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?)$ { location ~* \.(?:css|js|jpg|jpeg|gif|png|svg|ico|webp|woff2?)$ {
access_log off; access_log off;
expires 7d; expires 7d;