| # Stage 1: Build the Next.js frontend | |
| FROM node:20-alpine AS builder | |
| WORKDIR /app | |
| COPY package*.json ./ | |
| RUN npm install | |
| COPY . . | |
| # We need to ensure that the Next.js app knows the API URL during build/runtime | |
| # For this Hugging Face setup with NGINX, the API is available at the same domain under /api/ | |
| ENV NEXT_PUBLIC_API_URL=/api | |
| RUN npm run build | |
| # Stage 2: Setup Python environment and run both applications | |
| FROM python:3.11-slim | |
| WORKDIR /app | |
| # Install Node.js, NGINX, and Supervisord | |
| RUN apt-get update && apt-get install -y \ | |
| curl \ | |
| nginx \ | |
| supervisor \ | |
| && curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ | |
| && apt-get install -y nodejs \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Install Python dependencies | |
| COPY backend/requirements.txt /app/backend/ | |
| RUN pip install --no-cache-dir -r /app/backend/requirements.txt | |
| RUN pip install --no-cache-dir uvicorn | |
| # Copy built frontend from the builder stage | |
| COPY --from=builder /app/package*.json ./ | |
| COPY --from=builder /app/.next ./.next | |
| COPY --from=builder /app/public ./public | |
| COPY --from=builder /app/node_modules ./node_modules | |
| # Copy the rest of the frontend (e.g. next.config.ts) and the backend | |
| COPY . /app | |
| # Copy Nginx configuration | |
| COPY nginx.conf /etc/nginx/sites-available/default | |
| # Copy Supervisor configuration | |
| COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf | |
| # Expose the port Hugging Face Spaces expects | |
| EXPOSE 7860 | |
| # We set the environment variables for runtime | |
| ENV PORT=3000 | |
| ENV NEXT_PUBLIC_API_URL=/api | |
| # Start Supervisor, which will manage Next.js, FastAPI, and NGINX | |
| CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"] | |