From 8c53e8a9b01271d4e46a9101a99cbc532b900098 Mon Sep 17 00:00:00 2001 From: Arthur Belleville Date: Fri, 14 Nov 2025 23:16:27 +0100 Subject: [PATCH] Fix dockerfile --- apps/api/Dockerfile | 55 +++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/apps/api/Dockerfile b/apps/api/Dockerfile index 773d0ee..e1cb04f 100644 --- a/apps/api/Dockerfile +++ b/apps/api/Dockerfile @@ -3,6 +3,7 @@ # ============================================================================== # NOTE: This Dockerfile should be built from the monorepo root, not from apps/api # Build command: docker build -f apps/api/Dockerfile -t xtablo-api . +# Following pnpm Example 3 for CI/CD without BuildKit cache mounts FROM node:20-slim AS base # Configure pnpm @@ -10,50 +11,44 @@ ENV PNPM_HOME="/pnpm" ENV PATH="$PNPM_HOME:$PATH" RUN corepack enable -# Copy all files to working directory -COPY . /app +# ============================================================================== +# Production dependencies stage +# ============================================================================== +FROM base AS prod + +# Copy only lockfile first for better layer caching +COPY pnpm-lock.yaml /app/pnpm-lock.yaml WORKDIR /app -# ============================================================================== -# Production dependencies stage - Install only production dependencies -# ============================================================================== -FROM base AS prod-deps -RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile +# Fetch production dependencies (only needs lockfile) +RUN pnpm fetch --prod -# ============================================================================== -# Build stage - Install all dependencies and build -# ============================================================================== -FROM base AS build -RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile -# Build only the API and its dependencies +# Copy source files +COPY . /app + +# Install production dependencies from fetched cache +RUN pnpm install --prod --frozen-lockfile --offline + +# Build the API WORKDIR /app/apps/api RUN pnpm run build # ============================================================================== # Final stage - Production image # ============================================================================== -FROM node:20-slim - -# Configure pnpm -ENV PNPM_HOME="/pnpm" -ENV PATH="$PNPM_HOME:$PATH" -RUN corepack enable +FROM base # Create non-root user RUN groupadd --gid 1001 nodejs && \ useradd -l --uid 1001 --gid nodejs --shell /bin/bash --create-home nodejs -# Copy production dependencies from prod-deps stage -COPY --from=prod-deps /app/node_modules /app/node_modules -COPY --from=prod-deps /app/packages /app/packages - -# Copy built application from build stage -COPY --from=build /app/apps/api/dist /app/apps/api/dist -COPY --from=build /app/apps/api/package.json /app/apps/api/package.json - -# Copy workspace files for proper resolution -COPY --from=build /app/pnpm-workspace.yaml /app/pnpm-workspace.yaml -COPY --from=build /app/package.json /app/package.json +# Copy production dependencies and built application from prod stage +COPY --from=prod /app/node_modules /app/node_modules +COPY --from=prod /app/packages /app/packages +COPY --from=prod /app/apps/api/dist /app/apps/api/dist +COPY --from=prod /app/apps/api/package.json /app/apps/api/package.json +COPY --from=prod /app/pnpm-workspace.yaml /app/pnpm-workspace.yaml +COPY --from=prod /app/package.json /app/package.json # Set working directory WORKDIR /app/apps/api