nodejs.tmpl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # Auto-generated Dockerfile for Node.js application
  2. # Generated by BYOP Engine - Node.js Stack Analyzer
  3. FROM node:{{.NodeVersion}}-alpine
  4. # Set working directory
  5. WORKDIR /app
  6. {{if .SystemDeps}}
  7. # Install system dependencies
  8. RUN apk add --no-cache {{.SystemDeps | join " "}}
  9. {{end}}
  10. # Copy package files for better dependency caching
  11. COPY package.json ./
  12. {{if .HasYarnLock}}
  13. COPY yarn.lock ./
  14. {{else if .HasPnpmLock}}
  15. COPY pnpm-lock.yaml ./
  16. {{else if .HasPackageLock}}
  17. COPY package-lock.json ./
  18. {{end}}
  19. # Install dependencies based on package manager
  20. {{if eq .PackageManager "yarn"}}
  21. RUN yarn install --frozen-lockfile {{if .ProductionOnly}}--production{{end}}
  22. {{else if eq .PackageManager "pnpm"}}
  23. RUN corepack enable && pnpm install --frozen-lockfile {{if .ProductionOnly}}--prod{{end}}
  24. {{else}}
  25. {{if .ProductionOnly}}
  26. RUN npm ci --only=production
  27. {{else}}
  28. RUN npm ci
  29. {{end}}
  30. {{end}}
  31. # Copy source code
  32. COPY . .
  33. {{if .HasBuildScript}}
  34. # Build the application
  35. {{if eq .PackageManager "yarn"}}
  36. RUN yarn build
  37. {{else if eq .PackageManager "pnpm"}}
  38. RUN pnpm build
  39. {{else}}
  40. RUN npm run build
  41. {{end}}
  42. {{end}}
  43. {{if .PruneDevDeps}}
  44. # Remove development dependencies after build
  45. {{if eq .PackageManager "yarn"}}
  46. RUN yarn install --frozen-lockfile --production && yarn cache clean
  47. {{else if eq .PackageManager "pnpm"}}
  48. RUN pnpm prune --prod
  49. {{else}}
  50. RUN npm prune --production
  51. {{end}}
  52. {{end}}
  53. # Create non-root user for security
  54. RUN addgroup -g 1001 -S nodejs && \
  55. adduser -S nodeuser -u 1001 && \
  56. chown -R nodeuser:nodejs /app
  57. # Switch to non-root user
  58. USER nodeuser
  59. # Expose port
  60. EXPOSE {{.Port}}
  61. {{if .HealthCheckEndpoint}}
  62. # Add health check
  63. HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  64. CMD wget --no-verbose --tries=1 --spider http://localhost:{{.Port}}{{.HealthCheckEndpoint}} || exit 1
  65. {{end}}
  66. # Start the application
  67. CMD {{.StartCommand | toJSON}}