# ========================================== # Production CPU Runtime Environment # ========================================== FROM python:3.12-slim # Install system dependencies, Redis server, OpenBLAS for CPU matrix math, and Node.js RUN apt-get update && apt-get install -y \ curl \ gnupg \ build-essential \ cmake \ pkg-config \ git \ # libopenblas-dev \ redis-server \ && curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \ && apt-get install -y nodejs \ && rm -rf /var/lib/apt/lists/* # Install PM2 globally to manage background processes RUN npm install -g pm2 # Setup non-root user for Hugging Face compliance (UID 1000) RUN useradd -m -u 1000 user WORKDIR /home/user/app # Force llama-cpp-python to compile cleanly for CPU optimization # ENV CMAKE_ARGS="-DGGML_BLAS=ON -DGGML_BLAS_VENDOR=OpenBLAS" # Create cache directory for Hugging Face model downloads and set ownership ENV HF_HOME=/home/user/app/.cache/huggingface RUN mkdir -p $HF_HOME # --- Setup Python Worker --- COPY requirements.txt . ENV CMAKE_BUILD_PARALLEL_LEVEL=1 RUN pip install -vvv --no-cache-dir -r requirements.txt COPY python-ai-service/ ./python-ai-service # --- Setup Node Backend --- COPY node-backend/package*.json ./node-backend/ RUN cd node-backend && npm ci --only=production RUN cd node-backend && npm install typescript COPY node-backend/ ./node-backend RUN cd node-backend && npx --package typescript tsc RUN cd node-backend && npm prune --production # --- Setup Pre-built Frontend Static Files --- # Copies directly from your react-frontend/dist folder COPY react-frontend/dist ./node-backend/public # Copy startup script COPY start.sh . RUN chmod +x /home/user/app/start.sh # Set environment variables ENV PORT=7860 ENV NODE_ENV=production ENV APP_ENV=production ENV REDIS_URL=redis://127.0.0.1:6379 # Set write/read permissions for the Hugging Face non-root user RUN chown -R user:user /home/user/app USER user # Create a PM2 ecosystem file to launch Redis, Node Backend, and Python Worker RUN echo 'module.exports = { \ apps: [ \ { \ name: "node-backend", \ script: "npm", \ args: ["run", "start"], \ cwd: "node-backend", \ env: { PORT: "7860", REDIS_URL: "redis://127.0.0.1:6379" } \ }, \ { \ name: "python-worker", \ script: "python", \ args: "main.py", \ cwd: "python-ai-service", \ env: { \ REDIS_URL: "redis://127.0.0.1:6379", \ PYTHONPATH: "/home/user/app/python-ai-service" \ } \ } \ ] \ };' > ecosystem.config.js # Expose Hugging Face Space default web port EXPOSE 7860 # Start Redis, Node, and Python simultaneously # CMD ["pm2-runtime", "ecosystem.config.js"] CMD ["./start.sh"]