document_agent / Dockerfile
Jai-rathore29's picture
Deploy: DocAgent backend (deterministic date-anomaly fix)
f65e025
Raw
History Blame Contribute Delete
2.74 kB
# syntax=docker/dockerfile:1
# ---------------------------------------------------------------------------
# Hugging Face Spaces image for the Document-Agent backend (free CPU tier).
#
# HF Spaces (Docker SDK) builds the Dockerfile at the repo ROOT with the repo
# root as the build context, so source paths are prefixed with backend/. HF
# runs the container as UID 1000 and routes traffic to the port declared as
# `app_port:` in README.md (8000 here).
#
# Free Spaces have NO persistent disk, so DATA_DIR is an ephemeral, user-owned
# directory: uploads / SQLite / the vector store reset on restart or rebuild.
# That is fine for a demo. The ML models are baked in at build time (same as
# the production image) so cold start is fast and there are no runtime
# downloads.
# ---------------------------------------------------------------------------
FROM python:3.12-slim
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
HF_HUB_DISABLE_SYMLINKS_WARNING=1 \
# Fixed, shared cache locations so the non-root runtime user finds the
# models baked in during build.
HF_HOME=/opt/models/hf \
EASYOCR_MODULE_PATH=/opt/models/easyocr \
TORCH_HOME=/opt/models/torch \
DATA_DIR=/data
# Native libs needed by docling / opencv / easyocr.
RUN apt-get update && apt-get install -y --no-install-recommends \
libgl1 \
libglib2.0-0 \
libgomp1 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Install CPU-only torch/torchvision FIRST so docling's torch dependency
# resolves to the lightweight CPU wheels instead of multi-GB CUDA builds.
RUN pip install --index-url https://download.pytorch.org/whl/cpu \
torch torchvision
COPY backend/requirements.txt .
RUN pip install -r requirements.txt
# Bake the models into the image.
COPY backend/scripts/prefetch_models.py scripts/prefetch_models.py
RUN mkdir -p /opt/models && python scripts/prefetch_models.py
# Backend app source (repo root is the build context on HF Spaces).
COPY backend/ .
# HF Spaces runs the container as UID 1000. Make the data dir and the baked
# model/app trees writable/readable by that user.
RUN useradd -m -u 1000 user \
&& mkdir -p /data \
&& chown -R user:user /data /app /opt/models
USER user
ENV HOME=/home/user
EXPOSE 8000
# Honour $PORT if a host injects it; default 8000 matches app_port in README.
HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \
CMD python -c "import os,urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://localhost:%s/api/health' % os.environ.get('PORT','8000')).status==200 else 1)"
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000}"]