William Mattingly commited on
Commit
fc77a0b
Β·
1 Parent(s): e576673

Refactor Dockerfile for improved clarity and usage instructions

Browse files

- Simplified comments and updated usage instructions for local and HuggingFace Spaces deployment.
- Adjusted database handling instructions and default command for running the application.
- Removed unnecessary user creation and streamlined dependency installation process.

Files changed (1) hide show
  1. Dockerfile +28 -59
Dockerfile CHANGED
@@ -1,80 +1,49 @@
1
- # ── Scripture Detector β€” HuggingFace Spaces compatible Dockerfile ─────────────
2
  #
3
- # Usage on HuggingFace Spaces
4
- # ───────────────────────────
5
- # 1. Push this repo to a HF Space (Docker SDK).
6
- # 2. Add the following secrets in Space Settings β†’ Variables and secrets:
7
- # SD_SECRET_KEY β†’ any long random string, e.g. `openssl rand -hex 32`
8
- # GEMINI_API_KEY β†’ your Google AI Studio key (optional; can be set in-app)
9
- #
10
- # Local usage (standard)
11
- # ──────────────────────
12
  # docker build -t scripture-detector .
13
- # docker run -p 7860:7860 scripture-detector
14
  #
15
- # Local usage with a persistent database (mount a host directory)
16
- # ───────────────────────────────────────────────────────────────
17
- # docker run -p 7860:7860 \
18
- # -v "$(pwd)/db_volume:/app/db" \
 
 
19
  # -e SD_DB_DIR=/app/db \
20
- # -e SCRIPTURE_DETECTOR_CACHE_DB=0 \
21
- # scripture-detector
 
 
 
22
  # ─────────────────────────────────────────────────────────────────────────────
23
 
24
  FROM python:3.12-slim
25
 
26
- # ── System dependencies ───────────────────────────────────────────────────────
27
- RUN apt-get update && apt-get install -y --no-install-recommends \
28
- git curl \
29
  && rm -rf /var/lib/apt/lists/*
 
30
 
31
- # ── Non-root user (required by HuggingFace Spaces) ───────────────────────────
32
- RUN useradd -m -u 1000 user
33
  WORKDIR /app
34
 
35
- # ── Install uv (fast Python package manager) ─────────────────────────────────
36
- RUN pip install --no-cache-dir uv
37
 
38
- # ── Dependencies (cached layer β€” only re-runs when pyproject.toml changes) ───
39
- COPY --chown=user:user pyproject.toml uv.lock* ./
40
- RUN uv sync --no-dev --frozen || uv sync --no-dev
41
 
42
- # ── Application source ────────────────────────────────────────────────────────
43
- COPY --chown=user:user . .
44
-
45
- # ── Runtime environment ───────────────────────────────────────────────────────
46
- # Per-user in-memory database (each browser session gets its own isolated DB).
47
- ENV SCRIPTURE_DETECTOR_CACHE_DB=1
48
 
49
- # Tell Flask/app.py to configure SameSite=None; Secure cookies so they work
50
- # inside HuggingFace's iframe embedding.
51
- ENV SD_BEHIND_PROXY=1
52
 
53
- # Bind to all interfaces; HF Spaces expects port 7860.
54
  ENV SD_HOST=0.0.0.0
55
  ENV SD_PORT=7860
56
 
57
- # SD_SECRET_KEY should be set as a HF Space Secret (not hardcoded here).
58
- # If absent, a random key is generated at startup β€” sessions reset on redeploy.
59
-
60
- # Gunicorn needs to see the app module
61
- ENV PYTHONUNBUFFERED=1
62
-
63
- # ── Non-root user ─────────────────────────────────────────────────────────────
64
- USER user
65
-
66
  EXPOSE 7860
67
 
68
- # ── Start gunicorn ────────────────────────────────────────────────────────────
69
- # Single worker: the per-session in-memory databases live in one process.
70
- # More than one worker would mean different workers have different session stores,
71
- # causing "I added a source but now it's gone" bugs.
72
- CMD ["uv", "run", "gunicorn", \
73
- "--worker-class", "gthread", \
74
- "--workers", "1", \
75
- "--threads", "4", \
76
- "--bind", "0.0.0.0:7860", \
77
- "--timeout", "120", \
78
- "--access-logfile", "-", \
79
- "--error-logfile", "-", \
80
- "app:app"]
 
1
+ # ── Scripture Detector ────────────────────────────────────────────────────────
2
  #
3
+ # Build:
 
 
 
 
 
 
 
 
4
  # docker build -t scripture-detector .
 
5
  #
6
+ # Run (ephemeral in-memory database β€” data resets on container restart):
7
+ # docker run -p 5001:5001 scripture-detector
8
+ #
9
+ # Run (persistent database β€” mount a host directory):
10
+ # docker run -p 5001:5001 \
11
+ # -v "$(pwd)/data_volume:/app/db" \
12
  # -e SD_DB_DIR=/app/db \
13
+ # -e GEMINI_API_KEY=your_key_here \
14
+ # scripture-detector python app.py
15
+ #
16
+ # The default CMD uses --cache-db (in-memory SQLite) which is ideal for
17
+ # workshops and demos where persistence across restarts is not needed.
18
  # ─────────────────────────────────────────────────────────────────────────────
19
 
20
  FROM python:3.12-slim
21
 
22
+ # Install git (required by Hugging Face Spaces build system) and uv
23
+ RUN apt-get update && apt-get install -y --no-install-recommends git \
 
24
  && rm -rf /var/lib/apt/lists/*
25
+ RUN pip install --no-cache-dir uv
26
 
 
 
27
  WORKDIR /app
28
 
 
 
29
 
30
+ # Copy dependency manifest first to leverage Docker layer caching.
31
+ # Dependencies are only re-installed when pyproject.toml / uv.lock changes.
32
+ COPY pyproject.toml uv.lock* ./
33
 
34
+ # Sync dependencies (no dev extras)
35
+ RUN uv sync --no-dev --frozen || uv sync --no-dev
 
 
 
 
36
 
37
+ # Copy application source
38
+ COPY . .
 
39
 
40
+ # Bind to all interfaces inside the container
41
  ENV SD_HOST=0.0.0.0
42
  ENV SD_PORT=7860
43
 
44
+ # Expose the Flask port
 
 
 
 
 
 
 
 
45
  EXPOSE 7860
46
 
47
+ # Default: run with --cache-db (in-memory database, no volume needed).
48
+ # To use a persistent database instead, override CMD and set SD_DB_DIR.
49
+ CMD ["uv", "run", "python", "app.py", "--cache-db"]