Fix dockerfile
This commit is contained in:
parent
3977b863f8
commit
8c53e8a9b0
1 changed files with 25 additions and 30 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue