File size: 2,064 Bytes
563aafd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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"]