# Multi-stage build: Frontend + Backend # Stage 1: Build Next.js frontend FROM node:20-alpine AS frontend-builder WORKDIR /app/frontend # Copy frontend package files COPY frontend/package*.json ./ # Install frontend dependencies RUN npm ci # Copy frontend source code COPY frontend/ . # Build Next.js app with standalone output # When NEXT_PUBLIC_API_URL is empty, frontend will use relative URLs (same domain) ENV NEXT_PUBLIC_API_URL="" ENV NODE_ENV=production RUN npm run build # Stage 2: Python backend with frontend FROM python:3.11-slim # Set working directory WORKDIR /app # Install system dependencies including Node.js for running Next.js RUN apt-get update && apt-get install -y \ gcc \ curl \ && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ && apt-get install -y nodejs \ && rm -rf /var/lib/apt/lists/* # Copy requirements first for better caching COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir -r requirements.txt # Copy backend application code COPY main.py . COPY config.py . COPY api/ ./api/ COPY services/ ./services/ COPY data/ ./data/ # Copy frontend standalone build from previous stage # Standalone mode includes all necessary dependencies COPY --from=frontend-builder /app/frontend/.next/standalone ./frontend COPY --from=frontend-builder /app/frontend/.next/static ./frontend/.next/static COPY --from=frontend-builder /app/frontend/public ./frontend/public # Go back to app root WORKDIR /app # Create output directory for generated images RUN mkdir -p assets/generated # Expose ports (Next.js on 3000, FastAPI on 8000) # Note: Hugging Face Spaces will route to port 8000, so we'll proxy Next.js through FastAPI EXPOSE 8000 3000 # Set environment variables ENV PYTHONUNBUFFERED=1 ENV PORT=8000 ENV NODE_ENV=production # Create startup script to run both services RUN echo '#!/bin/bash\n\ set -e\n\ # Start Next.js server in background (bind to 0.0.0.0 for internal access)\n\ cd /app/frontend && HOSTNAME=0.0.0.0 PORT=3000 node server.js &\n\ # Wait for Next.js to start\n\ sleep 2\n\ # Start FastAPI server (foreground)\n\ cd /app && uvicorn main:app --host 0.0.0.0 --port 8000\n\ ' > /app/start.sh && chmod +x /app/start.sh # Run both services CMD ["/app/start.sh"]