# ── TF Serving Starter — FastAPI Proxy Image ────────────────────────────────
# This Dockerfile builds the FastAPI proxy layer that sits in front of TF Serving.
# The TF Serving itself runs as a separate container (see docker-compose.yml).
#
# Build:   docker build -t tf-serving-proxy .
# Run:     docker run -p 8000:8000 --env-file .env tf-serving-proxy
# ─────────────────────────────────────────────────────────────────────────────

FROM python:3.11-slim

# ── System dependencies ───────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
        curl \
    && rm -rf /var/lib/apt/lists/*

# ── Working directory ─────────────────────────────────────────────────────────
WORKDIR /app

# ── Install Python dependencies ───────────────────────────────────────────────
# Copy only requirements first to leverage Docker layer cache.
COPY requirements.txt .

# Install a CPU-only subset for the proxy (no full TF needed in the proxy image).
# If you want the full TF install (larger image), remove the --find-links flag.
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir \
        fastapi==0.109.2 \
        uvicorn[standard]==0.27.1 \
        httpx==0.26.0 \
        pydantic==2.6.1 \
        pydantic-settings==2.2.1 \
        pyyaml==6.0.1 \
        python-dotenv==1.0.1 \
        numpy==1.26.4

# ── Copy application source ───────────────────────────────────────────────────
COPY config/ config/
COPY src/    src/
COPY api/    api/

# ── Environment defaults (override at runtime via docker run --env-file .env) ─
ENV TF_SERVING_HOST=tf-serving \
    TF_SERVING_REST_PORT=8501 \
    PROXY_PORT=8000 \
    LOG_LEVEL=INFO

# ── Health check ──────────────────────────────────────────────────────────────
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD curl -f http://localhost:${PROXY_PORT}/health || exit 1

# ── Entrypoint ────────────────────────────────────────────────────────────────
EXPOSE 8000
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
