File size: 2,279 Bytes
b7334a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f201243
 
 
 
 
b7334a4
f201243
 
b7334a4
 
 
f201243
 
 
 
 
 
 
 
b7334a4
 
 
a55114e
b7334a4
 
 
64ea851
 
 
 
 
b7334a4
 
 
f201243
 
 
 
b7334a4
 
 
f201243
 
 
 
b7334a4
 
 
 
 
594322a
 
 
 
b7334a4
 
 
f201243
b7334a4
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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"]