File size: 2,544 Bytes
0fd3d47
954551b
 
0fd3d47
 
 
 
 
 
954551b
0fd3d47
 
 
 
 
 
954551b
0fd3d47
 
 
 
954551b
 
0fd3d47
 
 
 
 
 
 
954551b
 
0fd3d47
 
 
 
954551b
 
0fd3d47
 
 
954551b
0fd3d47
 
954551b
 
 
d762720
 
0fd3d47
 
 
 
 
 
 
954551b
0fd3d47
 
 
 
 
 
 
 
954551b
 
0fd3d47
 
 
 
954551b
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
# --- Stage 1: build the frontend ---
FROM node:20-alpine AS frontend-builder
WORKDIR /app/frontend

COPY frontend/package.json frontend/package-lock.json ./
# ci rather than install: a lockfile is committed, and `npm install` is free to
# ignore it and resolve versions that were never tested.
RUN npm ci

COPY frontend/ ./

# Empty means "same origin". next/font and output:'export' inline NEXT_PUBLIC_*
# values at *build* time, so leaving this unset previously baked
# http://localhost:8000 into an image whose server listens on 7860 -- the bundle
# called a host that does not exist for the user.
ENV NEXT_PUBLIC_BACKEND_URL=""
ENV NODE_ENV=production

# `prebuild` stages the MediaPipe WASM runtime and model into public/models so
# both are served from this origin: the runtime is copied from node_modules (so
# its version always matches the installed package) and the model is downloaded.
RUN npm run build


# --- Stage 2: runtime ---
FROM python:3.11-slim-bookworm

# Hugging Face Spaces expects the process to run as uid 1000.
RUN useradd --create-home --uid 1000 appuser

WORKDIR /app

# gcc and libpq-dev are not needed: psycopg2-binary ships prebuilt wheels, and
# dropping them saves roughly 200MB. curl is kept for the healthcheck.
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl \
    && rm -rf /var/lib/apt/lists/*

COPY backend/pyproject.toml backend/README.md* ./
COPY backend/app ./app
RUN pip install --no-cache-dir .

COPY backend/alembic.ini ./
COPY backend/migrations ./migrations

COPY --from=frontend-builder /app/frontend/out ./static

RUN chown -R appuser:appuser /app

ENV PORT=7860 \
    ENVIRONMENT=production \
    DATABASE_URL="" \
    # Must be on: the container sits behind the Spaces proxy, and without it
    # every visitor shares a single rate-limit bucket.
    TRUST_PROXY_HEADERS=true \
    PYTHONUNBUFFERED=1

# FACEVISION_ADMIN_KEY is deliberately not set here. The application refuses to
# start in production without it, which is what prevents a deployment from
# silently running on a key that is public in the repository. Provide it as a
# runtime secret:
#   docker run -e FACEVISION_ADMIN_KEY=...
# On Hugging Face Spaces, add it under Settings -> Repository secrets.

USER appuser
EXPOSE 7860

# Meaningful now that /api/health actually probes the database.
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
    CMD curl -fsS http://127.0.0.1:7860/api/health || exit 1

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]