File size: 2,597 Bytes
fb6a24f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# gamer.co.il archive search — Hugging Face Spaces (Docker SDK)
#
# Layout at runtime:
#   - server.py + static/   (app, shipped in the Space repo)
#   - avatars/              (~36 MB, shipped in the Space repo)
#   - forum_emojis/         (~120 KB, shipped in the Space repo)
#   - posts.db              (~24 GB, fetched from a separate HF Dataset
#                            repo at image-build time — the Space repo
#                            free-tier cap is 1 GB so the DB cannot live
#                            alongside the app)
#
# Layer order is tuned for HF's build cache: everything stable comes first,
# small volatile things (app code) last. Editing server.py only re-runs
# the COPY layer, not the 24 GB DB fetch or the chown.
#
# Read-only at request time; no writes happen at runtime.

FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PIP_NO_CACHE_DIR=1

# zstd: <1 MB, used to decompress the chunked DB.
RUN apt-get update && apt-get install -y --no-install-recommends zstd \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# 1. Dependencies — cached unless requirements.txt changes.
COPY requirements.txt .
RUN pip install -r requirements.txt

# 2. The big DB layer — cached unless DB_REPO / DB_REVISION changes.
# Pull the chunked, zstd-compressed posts.db from the dataset repo and
# reassemble. Files end up readable by everyone (default 0644) so the
# non-root user added below doesn't need them chowned.
ARG DB_REPO=gamer-forums/archive-db
ARG DB_REVISION=main
RUN python -c "from huggingface_hub import snapshot_download; \
snapshot_download(repo_id='${DB_REPO}', repo_type='dataset', revision='${DB_REVISION}', \
                  local_dir='/tmp/db-chunks', allow_patterns=['posts.db.zst.*'])" \
 && cat /tmp/db-chunks/posts.db.zst.* | zstd -d -o /app/posts.db \
 && rm -rf /tmp/db-chunks \
 && chmod 0644 /app/posts.db \
 && ls -lh /app/posts.db

# 3. Non-root user — created BEFORE the small app COPYs so chown is fast.
RUN useradd -m -u 1000 user

# 4. Small static assets — only re-copied if the dirs change.
COPY --chown=user:user avatars/       ./avatars/
COPY --chown=user:user forum_emojis/  ./forum_emojis/

# 5. App code last — frequent edits only re-run these tiny COPYs.
COPY --chown=user:user static/        ./static/
COPY --chown=user:user server.py      .

ENV GAMER_DB=/app/posts.db \
    GAMER_AVATARS=/app/avatars \
    GAMER_EMOJIS=/app/forum_emojis

EXPOSE 7860
USER user

CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]