# ============================================================================= # GoHorse Backoffice - Ultra-Optimized Dockerfile with pnpm # Target: < 200MB final image # ============================================================================= # Stage 1: Base with pnpm FROM node:20-alpine AS base RUN corepack enable && corepack prepare pnpm@9.15.4 --activate RUN apk add --no-cache libc6-compat # Stage 2: Fetch dependencies (better cache utilization) FROM base AS deps WORKDIR /app COPY pnpm-lock.yaml ./ RUN pnpm fetch # Stage 3: Build FROM base AS builder WORKDIR /app # Copy lockfile and fetch cache COPY pnpm-lock.yaml ./ COPY --from=deps /app/node_modules ./node_modules # Copy package files and install COPY package.json ./ RUN pnpm install --frozen-lockfile --offline # Copy source and build COPY . . RUN pnpm build # Prune dev dependencies RUN pnpm prune --prod && \ pnpm store prune && \ rm -rf /root/.local/share/pnpm/store # ============================================================================= # Stage 4: Production - Minimal runtime (Distroless alternative: Alpine) # ============================================================================= FROM node:20-alpine AS production # Security: non-root user RUN addgroup -g 1001 -S nodejs && \ adduser -S nestjs -u 1001 -G nodejs WORKDIR /app # Copy only production artifacts COPY --from=builder --chown=nestjs:nodejs /app/dist ./dist COPY --from=builder --chown=nestjs:nodejs /app/node_modules ./node_modules COPY --from=builder --chown=nestjs:nodejs /app/package.json ./ # Remove unnecessary files to reduce size RUN find node_modules -name "*.md" -delete 2>/dev/null || true && \ find node_modules -name "*.d.ts" -delete 2>/dev/null || true && \ find node_modules -name "CHANGELOG*" -delete 2>/dev/null || true && \ find node_modules -name "LICENSE*" -delete 2>/dev/null || true && \ find node_modules -name "*.map" -delete 2>/dev/null || true && \ find node_modules -type d -name "test" -exec rm -rf {} + 2>/dev/null || true && \ find node_modules -type d -name "tests" -exec rm -rf {} + 2>/dev/null || true && \ find node_modules -type d -name "__tests__" -exec rm -rf {} + 2>/dev/null || true && \ find node_modules -type d -name "docs" -exec rm -rf {} + 2>/dev/null || true && \ rm -rf /tmp/* /var/cache/apk/* # Environment ENV NODE_ENV=production ENV PORT=3001 ENV HOST=0.0.0.0 # Switch to non-root user USER nestjs EXPOSE 3001 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ CMD node -e "const http = require('http'); http.get('http://localhost:3001/health', (r) => process.exit(r.statusCode === 200 ? 0 : 1)).on('error', () => process.exit(1))" CMD ["node", "dist/main.js"]