# Dockerfile - DocTR Tuning REST API
#
# Build:
#   docker build -t doctr-api:latest .
#
# Run:
#   docker run -p 8003:8000 -v ./dataset:/app/dataset doctr-api:latest

FROM python:3.11-slim

LABEL maintainer="Sergio Jimenez"
LABEL description="DocTR Tuning REST API"

WORKDIR /app

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV DOCTR_DET_ARCH=db_resnet50
ENV DOCTR_RECO_ARCH=crnn_vgg16_bn

# Install system dependencies for OpenCV and image processing
RUN apt-get update && apt-get install -y --no-install-recommends \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender1 \
    && rm -rf /var/lib/apt/lists/*

# Copy and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY doctr_tuning_rest.py .
COPY dataset_manager.py .

# Volume for dataset and model cache
VOLUME ["/app/dataset", "/root/.cache/doctr"]

# Expose API port
EXPOSE 8000

# Health check (longer start period for model download)
HEALTHCHECK --interval=30s --timeout=10s --start-period=180s --retries=3 \
    CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1

# Run the API server
CMD ["uvicorn", "doctr_tuning_rest:app", "--host", "0.0.0.0", "--port", "8000"]
