File size: 2,212 Bytes
bc443ab
3f803ab
bc443ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f803ab
bc443ab
3f803ab
bc443ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Stage 1: Build Frontend
FROM node:20-alpine AS frontend-builder
WORKDIR /app/frontend

# Install dependencies
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci

# Copy source code and build
COPY frontend ./
# Disable telemetry during build
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build

# Stage 2: Setup Backend and Runtime
FROM python:3.8-slim

# Install system dependencies for opencv-python (libgl1), nodejs (runtime), and build tools
RUN apt-get update && apt-get install -y \
    libgl1-mesa-glx \
    libglib2.0-0 \
    curl \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

# Install Node.js 20 (required to run Next.js production server)
# Using nodesource setup script
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
    && apt-get install -y nodejs

WORKDIR /app

# Setup Backend dependencies
COPY backend/requirements.txt ./backend/
# Upgrade pip and install requirements.
# Pointing to torch wheels just in case, though 1.7.0 should be on PyPI for 3.8
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r backend/requirements.txt -f https://download.pytorch.org/whl/torch_stable.html

# Copy Backend Source
COPY backend ./backend

# Setup Frontend from Builder
WORKDIR /app/frontend
COPY --from=frontend-builder /app/frontend/.next ./.next
COPY --from=frontend-builder /app/frontend/public ./public
COPY --from=frontend-builder /app/frontend/package.json ./package.json
COPY --from=frontend-builder /app/frontend/package-lock.json ./package-lock.json
COPY --from=frontend-builder /app/frontend/node_modules ./node_modules
COPY --from=frontend-builder /app/frontend/next.config.mjs ./next.config.mjs

# Go back to root
WORKDIR /app

# Expose the mandatory Space port
EXPOSE 7860

# Create start script
# We start Flask on 5001 (matching the rewrite proxy)
# We start Next.js on 7860 (listening to the world)
RUN echo "#!/bin/bash\n\
# Start Flask in background\n\
python backend/flask_backend.py --port 5001 --host 127.0.0.1 &\n\
# Wait a bit for backend to initialize\n\
sleep 5\n\
# Start Next.js in foreground on port 7860\n\
cd frontend && npm start -- -p 7860\n\
" > start.sh && chmod +x start.sh

CMD ["./start.sh"]