Dockerfile 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ---
  2. name: Basic Web App Dockerfile
  3. description: Multi-stage Dockerfile for Node.js web applications with security best practices
  4. author: xcad (Christian Lempa)
  5. date: 2025-08-29
  6. module: docker
  7. files:
  8. - .dockerignore
  9. - docker-compose.yml
  10. ---
  11. # Multi-stage build for Node.js application
  12. FROM node:18-alpine AS builder
  13. # Set working directory
  14. WORKDIR /app
  15. # Copy package files
  16. COPY package*.json ./
  17. # Install dependencies
  18. RUN npm ci --only=production
  19. # Copy source code
  20. COPY . .
  21. # Build application
  22. RUN npm run build
  23. # Production stage
  24. FROM node:18-alpine AS production
  25. # Create non-root user
  26. RUN addgroup -g 1001 -S nodejs && \
  27. adduser -S nextjs -u 1001
  28. # Set working directory
  29. WORKDIR /app
  30. # Copy built application from builder stage
  31. COPY --from=builder --chown=nextjs:nodejs /app/dist ./dist
  32. COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
  33. COPY --from=builder --chown=nextjs:nodejs /app/package.json ./package.json
  34. # Switch to non-root user
  35. USER nextjs
  36. # Expose port
  37. EXPOSE 3000
  38. # Health check
  39. HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  40. CMD curl -f http://localhost:3000/health || exit 1
  41. # Start application
  42. CMD ["npm", "start"]