diff --git a/Dockerfile b/Dockerfile index e5623e8..f4dc6e7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,42 +1,11 @@ # syntax=docker/dockerfile:1.6 ############################ -# Stage 0: Frontend (Vite) +# Stage 1: PHP (Composer) # ############################ -ARG FRONTEND_DIR=. -FROM node:20-alpine AS nodebuild -ARG FRONTEND_DIR -WORKDIR /app - -# Toolchain for native node deps (safe to have) -RUN apk add --no-cache python3 make g++ - -# 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 package.json has "build": "vite build" (Laravel default with laravel-vite-plugin), this works: -RUN npm run build - -############################ -# Stage 1: PHP composer -############################ -FROM php:8.4-fpm-alpine AS build +FROM php:8.4-fpm-alpine AS phpbuild +# Build deps for PHP extensions + composer RUN set -eux; \ apk add --no-cache \ git unzip \ @@ -53,13 +22,38 @@ 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 ./ RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --no-scripts + +# Copy full app source (now vendor/ exists for later stages) COPY . . ############################ -# Stage 2: Runtime (nginx+php-fpm) +# Stage 2: Frontend (Vite) # ############################ +FROM node:20-alpine AS nodebuild +WORKDIR /app + +# Bring in the **already-composed** app (includes vendor/) +COPY --from=phpbuild /app /app + +# Native build tools in case any npm deps need them +RUN apk add --no-cache python3 make g++ + +# JS deps & build (Laravel default expects package.json at repo root) +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 + +ENV NODE_ENV=production +# Standard Laravel: "build": "vite build" +RUN npm run build + +##################################### +# Stage 3: Runtime (nginx + php-fpm)# +##################################### FROM php:8.4-fpm-alpine # Base runtime packages @@ -68,7 +62,7 @@ RUN set -eux; \ nginx supervisor curl \ libzip icu-libs -# Build PHP runtime extensions and install libpq runtime for pdo_pgsql/pgsql +# Build PHP runtime extensions and install libpq (needed by pdo_pgsql/pgsql) RUN set -eux; \ apk add --no-cache libzip-dev icu-dev oniguruma-dev postgresql-dev; \ docker-php-ext-configure zip; \ @@ -89,19 +83,15 @@ RUN set -eux; \ echo "realpath_cache_ttl=600"; \ } >> "$PHP_INI_DIR/conf.d/99-opcache.ini" -# PHP-FPM: pass env, listen TCP, run as nginx +# Configure 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; \ sed -ri 's|^user\s*=.*|user = 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 -# Create dirs +# Directories RUN set -eux; \ mkdir -p /run/nginx /var/log/nginx /var/log/supervisor \ /etc/nginx/conf.d /var/www/html \ @@ -110,12 +100,12 @@ RUN set -eux; \ WORKDIR /var/www/html # App code from composer stage -COPY --from=build --chown=nginx:nginx /app /var/www/html +COPY --from=phpbuild --chown=nginx:nginx /app /var/www/html -# Built frontend assets from node stage +# Built frontend assets from node stage (public/build/** including manifest.json) COPY --from=nodebuild --chown=nginx:nginx /app/public/build /var/www/html/public/build -# Nginx + Supervisor configs + health + entrypoint (these must exist in your repo) +# Nginx + Supervisor configs + health + entrypoint (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 @@ -123,23 +113,26 @@ 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 -# Logs to STDOUT/ERR +# Log 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 +# 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; \ chmod -R ug+rwX storage bootstrap/cache -# Runtime env + port +# Env + port 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"]