# Multi-stage build for production FROM node:18-alpine AS builder # Set working directory WORKDIR /app # Copy package files COPY package*.json ./ # Install dependencies RUN npm ci --only=production && npm cache clean --force # Copy source code COPY . . # Build the application RUN npm run build # Production stage FROM node:18-alpine AS production # Install dumb-init for proper signal handling RUN apk add --no-cache dumb-init # Create app user RUN addgroup -g 1001 -S nodejs RUN adduser -S nextjs -u 1001 # Set working directory WORKDIR /app # 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 --chown=nextjs:nodejs /app/dist ./dist COPY --from=builder --chown=nextjs:nodejs /app/server.ts ./ COPY --from=builder --chown=nextjs:nodejs /app/src ./src # Create necessary directories RUN mkdir -p /app/logs && chown nextjs:nodejs /app/logs # Switch to non-root user USER nextjs # Expose port EXPOSE 3001 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:3001/health || exit 1 # Use dumb-init to handle signals properly ENTRYPOINT ["dumb-init", "--"] # Start the server CMD ["npm", "start"]