1+ # Multi-stage build for PFControl v2
2+ FROM node:20-alpine AS builder
3+
4+ # Set working directory
5+ WORKDIR /app
6+
7+ # Copy package files
8+ COPY package*.json ./
9+
10+ # Install ALL dependencies (including dev dependencies for build)
11+ RUN npm ci --legacy-peer-deps && npm cache clean --force
12+
13+ # Copy source code
14+ COPY . .
15+
16+ # Copy frontend env for Vite build
17+ COPY .env.vite.preview .env.preview
18+
19+ # Build the backend (TypeScript compilation)
20+ RUN npm run build:server
21+
22+ # Build the application (frontend)
23+ RUN npm run build
24+
25+ # Production stage
26+ FROM node:20-alpine AS production
27+
28+ # Install dumb-init for proper signal handling
29+ RUN apk add --no-cache dumb-init
30+
31+ # Create app user for security
32+ RUN addgroup -g 1001 -S nodejs && \
33+ adduser -S nodeuser -u 1001
34+
35+ # Set working directory
36+ WORKDIR /app
37+
38+ # Copy package files
39+ COPY package*.json ./
40+
41+ # Install only production dependencies
42+ RUN npm ci --omit=dev --legacy-peer-deps && npm cache clean --force
43+
44+ # Set NODE_ENV explicitly
45+ ENV NODE_ENV=production
46+
47+ # Copy built application from builder stage
48+ COPY --from=builder --chown=nodeuser:nodejs /app/dist ./dist
49+ COPY --from=builder --chown=nodeuser:nodejs /app/public ./public
50+ COPY --from=builder --chown=nodeuser:nodejs /app/server/dist ./server/dist
51+ COPY --from=builder --chown=nodeuser:nodejs /app/server/data ./server/data
52+ COPY --from=builder --chown=nodeuser:nodejs /app/server/data ./server/dist/data
53+
54+ # Create logs directory
55+ RUN mkdir -p logs && chown nodeuser:nodejs logs
56+
57+ # Switch to non-root user
58+ USER nodeuser
59+
60+ # Expose port
61+ EXPOSE 9902
62+
63+ # Health check
64+ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
65+ CMD node -e "require('http').get('http://localhost:9902/health', (res) => { process.exit(res.statusCode === 200 ? 0 : 1) }).on('error', () => process.exit(1))"
66+ # Start the application
67+ ENTRYPOINT ["dumb-init", "--"]
68+ CMD ["node", "server/dist/main.js"]
0 commit comments