From f99ecced9fc415d1ec53daf0df1fdac4625e5f63 Mon Sep 17 00:00:00 2001 From: u00lipp Date: Wed, 22 Oct 2025 22:58:32 +0200 Subject: [PATCH] fix: build --- Dockerfile | 112 ++++++++++++++++++++++++----------------------------- 1 file changed, 50 insertions(+), 62 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6bda062..e5623e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,41 +1,49 @@ -# ========= -# Stage 0: Frontend build (Vite) -# ========= +# syntax=docker/dockerfile:1.6 + +############################ +# Stage 0: Frontend (Vite) +############################ +ARG FRONTEND_DIR=. FROM node:20-alpine AS nodebuild +ARG FRONTEND_DIR WORKDIR /app -# Install JS deps first for caching -COPY package.json package-lock.json* pnpm-lock.yaml* yarn.lock* ./ -RUN \ - if [ -f package-lock.json ]; then npm ci; \ - elif [ -f pnpm-lock.yaml ]; then corepack enable && pnpm i --frozen-lockfile; \ - elif [ -f yarn.lock ]; then yarn install --frozen-lockfile; \ - else npm i; fi +# Toolchain for native node deps (safe to have) +RUN apk add --no-cache python3 make g++ -# Copy the rest and build -COPY . . +# Copy manifests (the path MUST exist; set FRONTEND_DIR correctly) +COPY ${FRONTEND_DIR}/package.json /app/package.json +# Copy one of the lockfiles if it exists (copy-all-then-use is simpler than conditional COPY) +# If you use pnpm or yarn, keep the right lockfile in the same directory as package.json. +COPY ${FRONTEND_DIR}/package-lock.json /app/package-lock.json +# If you don't have a package-lock.json, comment the line above and uncomment one of these: +# COPY ${FRONTEND_DIR}/pnpm-lock.yaml /app/pnpm-lock.yaml +# COPY ${FRONTEND_DIR}/yarn.lock /app/yarn.lock + +# Install deps (choose installer based on the lockfile you actually copied) +RUN if [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then corepack enable && pnpm i --frozen-lockfile; \ + elif [ -f yarn.lock ]; then yarn install --frozen-lockfile; \ + else npm i; fi + +# Copy the rest of the frontend sources and build +COPY ${FRONTEND_DIR}/ /app/ ENV NODE_ENV=production -# If your Vite build needs ASSET_URL or similar, set it here: -# ENV ASSET_URL=/ -RUN \ - if [ -f package.json ]; then \ - if npm run | grep -q " build"; then npm run build; \ - else echo "No build script in package.json"; fi; \ - fi +# If your package.json has "build": "vite build" (Laravel default with laravel-vite-plugin), this works: +RUN npm run build -# ========= -# Stage 1: Composer / PHP deps -# ========= +############################ +# Stage 1: PHP composer +############################ FROM php:8.4-fpm-alpine AS build -# Build dependencies for PHP extensions and composer operations RUN set -eux; \ apk add --no-cache \ git unzip \ libzip-dev oniguruma-dev icu-dev \ autoconf build-base postgresql-dev -# PHP extensions (include PostgreSQL) +# PHP extensions (PG + MySQL + zip/opcache/intl) RUN docker-php-ext-configure zip; \ docker-php-ext-install -j"$(nproc)" \ pdo_mysql pdo_pgsql pgsql zip opcache intl @@ -44,20 +52,14 @@ RUN docker-php-ext-configure zip; \ ENV COMPOSER_ALLOW_SUPERUSER=1 COMPOSER_HOME=/tmp/composer COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer -# Install PHP deps WORKDIR /app COPY composer.json composer.lock ./ -RUN set -eux; \ - composer install \ - --no-dev --no-interaction --prefer-dist \ - --optimize-autoloader --no-scripts - -# Copy app source (do not run artisan optimize here; we’ll clear/warmup at runtime if needed) +RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --no-scripts COPY . . -# ========= -# Stage 2: Runtime (nginx + php-fpm) -# ========= +############################ +# Stage 2: Runtime (nginx+php-fpm) +############################ FROM php:8.4-fpm-alpine # Base runtime packages @@ -66,18 +68,13 @@ RUN set -eux; \ nginx supervisor curl \ libzip icu-libs -# Build PHP runtime extensions (need *-dev to compile) +# Build PHP runtime extensions and install libpq runtime for pdo_pgsql/pgsql RUN set -eux; \ - apk add --no-cache \ - libzip-dev icu-dev oniguruma-dev postgresql-dev; \ + apk add --no-cache libzip-dev icu-dev oniguruma-dev postgresql-dev; \ docker-php-ext-configure zip; \ - docker-php-ext-install -j"$(nproc)" \ - pdo_mysql pdo_pgsql pgsql zip opcache intl; \ - # PostgreSQL client runtime lib so pdo_pgsql/pgsql can load + docker-php-ext-install -j"$(nproc)" pdo_mysql pdo_pgsql pgsql zip opcache intl; \ apk add --no-cache libpq; \ - # Remove build deps - apk del --no-progress --purge \ - libzip-dev icu-dev oniguruma-dev postgresql-dev || true + apk del --no-progress --purge libzip-dev icu-dev oniguruma-dev postgresql-dev || true # php.ini production + opcache tuning RUN set -eux; \ @@ -92,7 +89,7 @@ RUN set -eux; \ echo "realpath_cache_ttl=600"; \ } >> "$PHP_INI_DIR/conf.d/99-opcache.ini" -# Configure PHP-FPM: pass env, listen on TCP, run as nginx +# PHP-FPM: pass env, listen TCP, run as 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; \ @@ -104,21 +101,21 @@ RUN set -eux; \ echo "catch_workers_output = yes"; \ } >> /usr/local/etc/php-fpm.d/www.conf -# Create required directories +# Create dirs RUN set -eux; \ mkdir -p /run/nginx /var/log/nginx /var/log/supervisor \ /etc/nginx/conf.d /var/www/html \ /var/cache/nginx /var/lib/nginx/tmp -# App code WORKDIR /var/www/html + +# App code from composer stage COPY --from=build --chown=nginx:nginx /app /var/www/html -# Copy built frontend assets from node stage +# Built frontend assets from node stage COPY --from=nodebuild --chown=nginx:nginx /app/public/build /var/www/html/public/build -# Nginx + Supervisor configs + health + entrypoint -# Ensure these files exist in your repo under ./docker +# 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 @@ -126,32 +123,23 @@ 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 -# Log to STDOUT/ERR +# Logs to STDOUT/ERR RUN set -eux; \ ln -sf /dev/stdout /var/log/nginx/access.log; \ ln -sf /dev/stderr /var/log/nginx/error.log -# Laravel writable dirs & permissions +# Laravel writable dirs 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 /var/lib/nginx/tmp \ - /run/nginx /var/log/nginx /var/log/supervisor; \ + /var/cache/nginx /var/lib/nginx /run/nginx /var/log/nginx /var/log/supervisor; \ chmod -R ug+rwX storage bootstrap/cache -# Optional: clear caches on container start via entrypoint (your entrypoint can do this) -# e.g., in /usr/local/bin/entrypoint.sh you might have: -# php artisan config:clear || true && php artisan cache:clear || true - -# Env + port +# Runtime env + port ENV APP_ENV=production APP_DEBUG=false APP_URL=http://localhost APP_PORT=8080 EXPOSE 8080 -# Healthcheck HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD ["/usr/local/bin/healthcheck.sh"] -# Run as nginx user USER nginx:nginx - -# Launch supervisor (which can start php-fpm + nginx; your supervisord.conf should do that) CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]