# Stage 1: Build FROM node:20-alpine AS builder WORKDIR /app # Copy package files COPY package*.json ./ COPY prisma ./prisma/ # Install dependencies RUN npm ci # Copy source code COPY . . # Generate Prisma client (dummy DATABASE_URL for build time only) RUN DATABASE_URL="postgresql://user:pass@localhost:5432/db" npx prisma generate # Build the application RUN npm run build # Stage 2: Production - use Debian slim for OpenSSL compatibility FROM node:20-slim AS production WORKDIR /app # Install OpenSSL and wget for health checks RUN apt-get update && apt-get install -y --no-install-recommends \ openssl \ wget \ && rm -rf /var/lib/apt/lists/* # Create non-root user for security RUN groupadd -g 1001 nodejs && \ useradd -u 1001 -g nodejs nestjs # Copy package files COPY package*.json ./ # Install production dependencies only RUN npm ci --only=production && npm cache clean --force # Copy built application COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma COPY --from=builder /app/prisma ./prisma # Set ownership RUN chown -R nestjs:nodejs /app # Switch to non-root user USER nestjs # Expose port EXPOSE 3002 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:3002/health || exit 1 # Start the application CMD ["node", "dist/src/main.js"]