# Stage 1: Build React frontend FROM node:20-alpine AS frontend-builder WORKDIR /app/frontend # Install build dependencies for native modules RUN apk add --no-cache python3 make g++ # Set npm config for better performance and caching RUN npm config set registry https://registry.npmjs.org/ RUN npm config set fetch-timeout 300000 RUN npm config set fetch-retry-mintimeout 20000 RUN npm config set fetch-retry-maxtimeout 120000 RUN npm config set cache /tmp/.npm # Copy package files first for better caching COPY frontend/package*.json ./ # Clean install dependencies to fix rollup issue with optimized flags RUN rm -rf node_modules package-lock.json RUN npm install --no-audit --no-fund --prefer-offline --build-from-source --production=false --cache /tmp/.npm # Copy source code (only what's needed for build) COPY frontend/src/ ./src/ COPY frontend/public/ ./public/ COPY frontend/index.html ./ COPY frontend/vite.config.js ./ COPY frontend/eslint.config.js ./ # Build the application with optimized settings RUN npm run build # Stage 2: Setup Node.js backend and serve everything FROM node:20-alpine WORKDIR /app # Set npm config for better performance and caching RUN npm config set registry https://registry.npmjs.org/ RUN npm config set fetch-timeout 300000 RUN npm config set cache /tmp/.npm # Copy package files first for better caching COPY backend/package*.json ./ # Install production dependencies only with optimized flags RUN npm ci --only=production --no-audit --no-fund --prefer-offline --cache /tmp/.npm # Copy backend source code (only what's needed) COPY backend/server.js ./ COPY backend/package.json ./ # Copy built frontend assets from the previous stage COPY --from=frontend-builder /app/frontend/dist ./public # Create non-root user for security RUN addgroup -g 1001 -S nodejs RUN adduser -S nodejs -u 1001 RUN chown -R nodejs:nodejs /app USER nodejs # The port the backend runs on EXPOSE 3001 # Start the server CMD ["node", "server.js"]