# Multi-stage Dockerfile for NestJS application # Stage 1: Build stage FROM node:18-alpine AS builder WORKDIR /app # Copy package files COPY package*.json ./ # Install all dependencies (including dev dependencies) RUN npm ci # Copy source code COPY . . # Build the application RUN npm run build # Stage 2: Production stage FROM node:18-alpine AS production WORKDIR /app # Create non-root user for security RUN addgroup -g 1001 -S nodejs RUN 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 # Change ownership of the app directory to the nestjs user RUN chown -R nestjs:nodejs /app USER nestjs # Expose port EXPOSE 3000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD node -e "require('http').get('http://localhost:3000/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) })" # Start the application CMD ["node", "dist/main"]