# ── Stage 1: Download embedding model ─────────────────────────────────────── # Model is pulled once at build time and copied into the runtime image. # This eliminates the 274MB download on every cold start. FROM python:3.12.8-slim AS model-downloader RUN apt-get update && apt-get install -y --no-install-recommends curl zstd \ && rm -rf /var/lib/apt/lists/* RUN curl -fsSL https://ollama.ai/install.sh | sh # Start Ollama, wait until ready, then pull the embedding model. # Loop exits with code 1 if Ollama never starts — fails the build immediately. RUN ollama serve & \ OLLAMA_PID=$! && \ for i in $(seq 1 30); do \ if curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then \ ollama pull nomic-embed-text && \ kill $OLLAMA_PID 2>/dev/null || true && \ exit 0; \ fi; \ sleep 1; \ done; \ echo "ERROR: Ollama failed to start after 30s" && \ kill $OLLAMA_PID 2>/dev/null || true && \ exit 1 # ── Stage 2: Runtime image ─────────────────────────────────────────────────── FROM python:3.12.8-slim WORKDIR /app # System deps: # - curl/zstd: needed by Ollama installer + entrypoint health poll # - pango/cairo/gdk-pixbuf: runtime deps for WeasyPrint PDF rendering # - libglib2.0-0/libharfbuzz/libfontconfig: pulled by pango but listed explicitly # NOTE: libmupdf-dev and gcc are intentionally omitted — PyMuPDF ships a # pre-built wheel (bundled MuPDF) so no compilation is needed. RUN apt-get update && \ apt-get install -y --no-install-recommends \ curl \ zstd \ libpango-1.0-0 \ libpangoft2-1.0-0 \ libgdk-pixbuf-2.0-0 \ libcairo2 \ libfontconfig1 \ && rm -rf /var/lib/apt/lists/* # Python dependencies first — so pip errors appear before Ollama download noise. # # REQUIREMENTS_FILE selects the dependency set. # # The DEFAULT is the full set, deliberately. This Dockerfile is synced verbatim # to the Hugging Face Space by scripts/sync-chatbot.sh, and HF builds it with no # build args — so whatever is default here is what runs there. Defaulting to the # lean set would silently strip Surya from that environment as a side effect of # an unrelated commit. # # requirements.txt (default) — full set, including the Surya OCR # fallback. Correct for development and for the HF Space. # # requirements-prod.txt — VPS build, selected explicitly by the GitHub # Actions workflow. Omits surya-ocr / transformers / torch, which add # 2.0-2.5 GB of RSS: that does not fit the 8 GB host and would get # Postgres OOM-killed, and on 4 CPU-only vCPUs Surya runs at minutes per # page anyway. Google Vision is the only OCR engine there. # Image: ~1.5 GB instead of ~7 GB. # # docker build --build-arg REQUIREMENTS_FILE=requirements-prod.txt . ARG REQUIREMENTS_FILE=requirements.txt COPY requirements.txt requirements-prod.txt ./ RUN pip install --no-cache-dir -r "${REQUIREMENTS_FILE}" # Create non-root user RUN groupadd -r appuser && useradd -r -g appuser -m -d /home/appuser appuser # Install Ollama binary (no model pull — model comes from build stage) RUN curl -fsSL https://ollama.ai/install.sh | sh # Copy pre-downloaded model from builder into the image layer # No volume needed — model is baked in and survives container restarts RUN mkdir -p /home/appuser/.ollama/models COPY --from=model-downloader /root/.ollama/models /home/appuser/.ollama/models RUN chown -R appuser:appuser /home/appuser/.ollama # Download Amiri Arabic fonts from release zip (files moved from repo root in newer versions) # Uses Python (already in image) — no extra apt packages needed RUN python3 -c "\ import urllib.request, zipfile, io, os; \ os.makedirs('/app/app/assets/fonts/Amiri', exist_ok=True); \ url='https://github.com/aliftype/amiri/releases/download/1.000/Amiri-1.000.zip'; \ data=urllib.request.urlopen(url).read(); \ z=zipfile.ZipFile(io.BytesIO(data)); \ [open('/app/app/assets/fonts/Amiri/'+os.path.basename(n),'wb').write(z.read(n)) \ for n in z.namelist() if n.endswith('.ttf')]; \ print('Amiri fonts extracted:', os.listdir('/app/app/assets/fonts/Amiri')) \ " COPY . . RUN chown -R appuser:appuser /app # Startup script COPY docker-entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh # Ollama: bind to localhost only, use appuser's home for models ENV OLLAMA_HOST=127.0.0.1:11434 ENV OLLAMA_MODELS=/home/appuser/.ollama/models # Never evict a loaded model. Ollama's default is 5m, after which the next # request pays a cold load (llama-server start + model load, ~2.5s+) — which # overshoots the 2500ms retrieval timeout in routing_schemas.py and makes exam # generation silently fall back to "continuing without context". The embedding # model is only ~274MB, so keeping it resident is far cheaper than losing # retrieval on every request that follows an idle gap. ENV OLLAMA_KEEP_ALIVE=-1 USER appuser EXPOSE 8000 # Model is pre-downloaded — start-period can be short (no download on startup) HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1 CMD ["/entrypoint.sh"]