# Multi-stage build for production optimization FROM node:18-alpine as builder WORKDIR /app # Copy package files COPY package*.json ./ COPY tsconfig*.json ./ # Install dependencies RUN npm ci --only=production && npm cache clean --force # Copy source code COPY src/ ./src/ # Build the application RUN npm run build # Production stage FROM node:18-alpine as production WORKDIR /app # Install only production dependencies COPY package*.json ./ RUN npm ci --only=production && npm cache clean --force # Copy built application from builder stage COPY --from=builder /app/dist ./dist # Create non-root user for security RUN addgroup -g 1001 -S nodejs RUN adduser -S nestjs -u 1001 # Change ownership of the working directory RUN chown -R nestjs:nodejs /app USER nestjs # Expose port EXPOSE 3001 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD node dist/health-check.js || exit 1 # Start the application CMD ["node", "dist/main"]