- Simplified the installation of production dependencies by copying node_modules from the builder stage. - Enhanced the Dockerfile by removing unnecessary environment variable settings and comments for clarity. - Ensured all necessary built applications and dependencies are included in the final image.
119 lines
3.5 KiB
Docker
119 lines
3.5 KiB
Docker
# 🚀 Backend (BFF) Dockerfile
|
|
# Optimized multi-stage build for NestJS production
|
|
|
|
# =====================================================
|
|
# Dependencies Stage - Install all dependencies
|
|
# =====================================================
|
|
FROM node:22-alpine AS deps
|
|
|
|
# Install system dependencies
|
|
RUN apk add --no-cache libc6-compat
|
|
|
|
# Install pnpm
|
|
RUN corepack enable && corepack prepare pnpm@10.15.0 --activate
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy workspace configuration
|
|
COPY pnpm-workspace.yaml package.json pnpm-lock.yaml ./
|
|
|
|
# Copy package.json files for dependency resolution
|
|
COPY packages/shared/package.json ./packages/shared/
|
|
COPY apps/bff/package.json ./apps/bff/
|
|
|
|
# Install dependencies with frozen lockfile
|
|
RUN pnpm install --frozen-lockfile --prefer-offline
|
|
|
|
# =====================================================
|
|
# Builder Stage - Build the application
|
|
# =====================================================
|
|
FROM node:22-alpine AS builder
|
|
|
|
# Install pnpm
|
|
RUN corepack enable && corepack prepare pnpm@10.15.0 --activate
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy workspace configuration
|
|
COPY pnpm-workspace.yaml package.json pnpm-lock.yaml ./
|
|
|
|
# Copy source code
|
|
COPY packages/shared/ ./packages/shared/
|
|
COPY apps/bff/ ./apps/bff/
|
|
COPY tsconfig.json ./
|
|
|
|
# Copy node_modules from deps stage
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY --from=deps /app/packages/shared/node_modules ./packages/shared/node_modules
|
|
COPY --from=deps /app/apps/bff/node_modules ./apps/bff/node_modules
|
|
|
|
# Build shared package first
|
|
WORKDIR /app/packages/shared
|
|
RUN pnpm build
|
|
|
|
# Generate Prisma client first, then build BFF (TS needs generated types)
|
|
WORKDIR /app/apps/bff
|
|
RUN pnpm prisma generate && pnpm build
|
|
|
|
# =====================================================
|
|
# Production Stage - Final optimized image
|
|
# =====================================================
|
|
FROM node:22-alpine AS production
|
|
|
|
# Install runtime dependencies
|
|
RUN apk add --no-cache \
|
|
wget \
|
|
curl \
|
|
&& rm -rf /var/cache/apk/*
|
|
|
|
# Install pnpm for production dependencies
|
|
RUN corepack enable && corepack prepare pnpm@10.15.0 --activate
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy workspace configuration
|
|
COPY pnpm-workspace.yaml package.json pnpm-lock.yaml ./
|
|
COPY packages/shared/package.json ./packages/shared/
|
|
COPY apps/bff/package.json ./apps/bff/
|
|
|
|
# Copy built applications and dependencies from builder
|
|
COPY --from=builder /app/packages/shared/dist ./packages/shared/dist
|
|
COPY --from=builder /app/apps/bff/dist ./apps/bff/dist
|
|
COPY --from=builder /app/apps/bff/prisma ./apps/bff/prisma
|
|
|
|
# Copy node_modules from builder (includes all dependencies)
|
|
COPY --from=builder /app/node_modules ./node_modules
|
|
COPY --from=builder /app/packages/shared/node_modules ./packages/shared/node_modules
|
|
COPY --from=builder /app/apps/bff/node_modules ./apps/bff/node_modules
|
|
|
|
# Generate Prisma client in the production image (ensures engines and client are present)
|
|
WORKDIR /app/apps/bff
|
|
RUN pnpm prisma generate
|
|
|
|
# Create non-root user
|
|
RUN addgroup --system --gid 1001 nodejs && \
|
|
adduser --system --uid 1001 nestjs
|
|
|
|
# Create necessary directories
|
|
RUN mkdir -p /app/secrets /app/logs && \
|
|
chown -R nestjs:nodejs /app
|
|
|
|
# Switch to non-root user
|
|
USER nestjs
|
|
|
|
# Expose port
|
|
EXPOSE 4000
|
|
|
|
# Environment variables
|
|
ENV NODE_ENV=production
|
|
ENV PORT=4000
|
|
|
|
# Set working directory for the app
|
|
WORKDIR /app/apps/bff
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
|
CMD wget --no-verbose --tries=1 --spider http://localhost:4000/health || exit 1
|
|
|
|
# Start command
|
|
CMD ["node", "dist/main"] |