# STAGE 1: Build the React frontend with a fresh and clean dependency install FROM node:18-bullseye AS builder WORKDIR /app # Copy ONLY the frontend's package.json file to force a fresh install COPY web/package.json ./ # Run npm install (NOT ci) to create a new dependency tree from scratch. # This is the key step to fix a potentially corrupted lock file issue. RUN npm install # Copy the rest of the frontend's source code COPY web/ ./ # Increase the memory available to the Node.js process for the build. ENV NODE_OPTIONS=--max-old-space-size=4096 # Build the frontend. It will now have a clean and correct node_modules. RUN npm run build # STAGE 2: Create the final, lean production image FROM node:18-bullseye WORKDIR /app # Install System Dependencies required for the backend RUN apt-get update && apt-get install -y python3 python3-pip build-essential && rm -rf /var/lib/apt/lists/* # Copy only the necessary package manifests for the backend and root COPY package.json package-lock.json* ./ COPY backend/package.json ./backend/ COPY backend/python/requirements.txt ./backend/python/ # Install only production dependencies for the backend and root using npm ci RUN npm ci --omit=dev # Install Python dependencies RUN pip3 install --no-cache-dir -r backend/python/requirements.txt # Create the backend directory and ensure it's owned by 'node' before copying contents RUN mkdir -p /app/backend && chown node:node /app/backend # Switch to the non-root 'node' user BEFORE copying backend source USER node # Copy backend source code as 'node' user COPY backend/ ./backend/ # Pre-create the directories that the Node.js app needs to write to. # This ensures they exist with correct permissions before the app tries to create them. RUN mkdir -p /app/backend/uploads /app/backend/temp /app/backend/results # Copy the pre-built frontend from the builder stage COPY --from=builder /app/build ./web/build # --- Final Configuration --- ENV PORT=7860 EXPOSE 7860 # Define the command to start the production server CMD ["node", "backend/server.js"]