Dockerfile 951 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Multi-stage build for optimized image size
  2. FROM node:18-alpine AS builder
  3. WORKDIR /app
  4. # Copy package files
  5. COPY package*.json ./
  6. # Install dependencies
  7. RUN npm ci
  8. # Copy source files
  9. COPY tsconfig.json ./
  10. COPY src ./src
  11. # Build the project
  12. RUN npm run build
  13. # Production stage
  14. FROM node:18-alpine
  15. WORKDIR /app
  16. # Copy package files
  17. COPY package*.json ./
  18. # Install only production dependencies
  19. RUN npm ci --only=production && \
  20. npm cache clean --force
  21. # Copy built files from builder stage
  22. COPY --from=builder /app/dist ./dist
  23. # Create a non-root user
  24. RUN addgroup -g 1001 -S nodejs && \
  25. adduser -S nodejs -u 1001
  26. # Change ownership of the app directory
  27. RUN chown -R nodejs:nodejs /app
  28. # Switch to non-root user
  29. USER nodejs
  30. # Set environment variables
  31. ENV NODE_ENV=production
  32. # Expose port for HTTP transport mode
  33. # (Also available for health checks in stdio mode)
  34. EXPOSE 3000
  35. # Start the server
  36. CMD ["node", "dist/index.js"]