66 lines
No EOL
2.1 KiB
Docker
66 lines
No EOL
2.1 KiB
Docker
# ==============================================================================
|
|
# Base stage - Common dependencies and setup
|
|
# ==============================================================================
|
|
# 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
|
|
ENV PNPM_HOME="/pnpm"
|
|
ENV PATH="$PNPM_HOME:$PATH"
|
|
RUN corepack enable
|
|
|
|
# ==============================================================================
|
|
# 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
|
|
|
|
# Fetch production dependencies (only needs lockfile)
|
|
RUN pnpm fetch --prod
|
|
|
|
# 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 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 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
|
|
|
|
# Change ownership to nodejs user
|
|
RUN chown -R nodejs:nodejs /app
|
|
|
|
# Switch to non-root user
|
|
USER nodejs
|
|
|
|
# Expose port
|
|
EXPOSE 8080
|
|
|
|
# NODE_ENV will be set via Cloud Run environment variables
|
|
CMD ["pnpm", "start"] |