| # Multi-stage build for production | |
| # Stage 1: Build the React application | |
| FROM node:20-alpine AS builder | |
| WORKDIR /app | |
| # Copy package files | |
| COPY package.json package-lock.json* ./ | |
| # Install dependencies | |
| RUN npm ci --silent | |
| # Copy source code | |
| COPY . . | |
| # Build the application | |
| RUN npm run build | |
| # Stage 2: Serve with nginx | |
| FROM nginx:alpine | |
| # Install curl and gettext (for envsubst) for reliable health checks and env var substitution | |
| RUN apk add --no-cache curl gettext | |
| # Copy nginx template configuration | |
| COPY nginx.conf.template /etc/nginx/templates/default.conf.template | |
| # Copy entrypoint script | |
| COPY docker-entrypoint.sh /docker-entrypoint-custom.sh | |
| RUN chmod +x /docker-entrypoint-custom.sh | |
| # Set default API_PORT if not provided | |
| ENV API_PORT=8000 | |
| # Copy built assets from builder stage | |
| COPY --from=builder /app/dist /usr/share/nginx/html | |
| # Expose port | |
| EXPOSE 5173 | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ | |
| CMD curl -fsS http://localhost:5173/health || exit 1 | |
| # Use custom entrypoint that processes templates | |
| ENTRYPOINT ["/docker-entrypoint-custom.sh"] | |
| CMD ["nginx", "-g", "daemon off;"] | |