# --- STAGE 1: FRONTEND BUILD (Node.js) --- FROM node:20-slim AS frontend-builder WORKDIR /app # Increase memory for the JS builder to prevent "exit code: 2" crashes ENV NODE_OPTIONS="--max-old-space-size=4096" # Copy monorepo root config COPY frontend/package*.json ./frontend/ COPY frontend/tsconfig.json ./frontend/ # Copy all packages and apps COPY frontend/packages ./frontend/packages COPY frontend/apps ./frontend/apps WORKDIR /app/frontend # Install and build sequentially (More reliable than --workspaces) RUN npm install RUN npm run build -w apps/customer RUN npm run build -w apps/internal # --- STAGE 2: BACKEND BASE (Python) --- FROM python:3.11-slim AS base ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PYTHONPATH=/app WORKDIR /app # Install minimal system dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ libpq-dev \ gcc \ curl \ ca-certificates \ redis-server \ && apt-get clean && rm -rf /var/lib/apt/lists/* # Install core requirements COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # --- STAGE 3: HEAVY-AI --- FROM base AS heavy COPY requirements-ai.txt . RUN pip install --no-cache-dir -r requirements-ai.txt # --- STAGE 4: PRODUCTION (Unified) --- FROM heavy AS production WORKDIR /app # Create a non-privileged user and group RUN groupadd -r nexus && useradd -r -g nexus -u 1000 nexus # Create runtime directories and set ownership BEFORE copying code RUN mkdir -p uploads logs && \ chown -R nexus:nexus /app # Copy backend application code (Change ownership during copy) COPY --chown=nexus:nexus . . # Copy BUILT frontend assets from Stage 1 (Change ownership during copy) COPY --chown=nexus:nexus --from=frontend-builder /app/frontend/apps/customer/dist ./frontend/apps/customer/dist COPY --chown=nexus:nexus --from=frontend-builder /app/frontend/apps/internal/dist ./frontend/apps/internal/dist # Ensure the app can write to the runtime directories RUN chmod -R 775 uploads logs # Expose Hugging Face default port EXPOSE 7860 # Ensure entrypoint script is executable RUN sed -i 's/\r$//' scripts/run_prod.sh && \ chmod +x scripts/run_prod.sh # Switch to the non-root user USER nexus # Use the unified sidecar entrypoint CMD ["./scripts/run_prod.sh"]