# Build stage FROM node:20-alpine AS builder WORKDIR /app # Copy package files COPY package*.json ./ # Install dependencies RUN npm ci # Copy source code COPY . . # Build the application RUN npm run build # Production stage FROM node:20-alpine AS production WORKDIR /app # Create non-root user for security RUN addgroup -g 1001 -S nodejs && \ adduser -S nestjs -u 1001 # Copy package files COPY package*.json ./ # Install only production dependencies RUN npm ci --only=production && npm cache clean --force # Copy built application from builder stage COPY --from=builder /app/dist ./dist # Set ownership to non-root user RUN chown -R nestjs:nodejs /app # Switch to non-root user USER nestjs # Expose port EXPOSE 3001 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:3001/health || exit 1 # Set environment variables ENV NODE_ENV=production ENV PORT=3001 # Start the application CMD ["node", "-r", "tsconfig-paths/register", "dist/main.js"]