File size: 2,657 Bytes
38737cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Stage 1: Build React frontend
FROM node:20-slim AS frontend-builder

WORKDIR /app/frontend

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci

# Copy source files
COPY . .

# Build the React app
RUN npm run build

# Stage 2: Python backend + serve frontend
FROM python:3.12-slim-trixie

# Copy uv from the official distroless image (recommended approach)
COPY --from=ghcr.io/astral-sh/uv:0.9.15 /uv /uvx /bin/

# Install system dependencies for Playwright and nginx
RUN apt-get update && apt-get install -y \
    nginx \
    supervisor \
    libnss3 \
    libnspr4 \
    libatk1.0-0 \
    libatk-bridge2.0-0 \
    libcups2 \
    libdrm2 \
    libxkbcommon0 \
    libxcomposite1 \
    libxdamage1 \
    libxfixes3 \
    libxrandr2 \
    libgbm1 \
    libasound2 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libcairo2 \
    libatspi2.0-0 \
    xvfb \
    fonts-liberation \
    libappindicator3-1 \
    libu2f-udev \
    libvulkan1 \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Create a new user named "user" with user ID 1000 (required for HF Spaces)
RUN useradd -m -u 1000 user

# Create necessary directories with proper permissions for nginx (before switching user)
RUN mkdir -p /var/log/nginx /var/lib/nginx /var/cache/nginx /run \
    && chown -R user:user /var/log/nginx /var/lib/nginx /var/cache/nginx /run \
    && chmod -R 755 /var/log/nginx /var/lib/nginx /var/cache/nginx /run

# Configure nginx (needs root for /etc/nginx)
COPY nginx.conf /etc/nginx/nginx.conf

# Configure supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Allow user to run supervisor
RUN chown -R user:user /etc/supervisor

# Switch to the "user" user
USER user

# Set home to the user's home directory
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

# Set the working directory to the user's home directory
WORKDIR $HOME/app

# Copy backend code and sync with locked dependencies
COPY --chown=user:user backend/ ./backend/
RUN cd backend && uv sync

# Copy FARA source
COPY --chown=user:user fara/ ./fara/

# Activate the virtual environment by adding it to PATH
ENV PATH="$HOME/app/backend/.venv/bin:$PATH"

# Install Playwright browsers
RUN playwright install chromium

# Copy built frontend from Stage 1
COPY --chown=user:user --from=frontend-builder /app/frontend/dist ./static

# Expose port
EXPOSE 7860

# Set environment variables
ENV PYTHONUNBUFFERED=1

# Start supervisor (manages nginx + python backend)
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]