# ── Base: exact patch version matching the training interpreter (§1) ────── # NOT alpine: musl libc has no manylinux wheels, so NumPy/SciPy would # compile from source and can differ numerically. FROM python:3.12-slim # ── Interpreter behaviour. Metadata only — no layer, never invalidated. ─── # PYTHONUNBUFFERED is not cosmetic: without it logs sit in a buffer and # vanish when the container is killed, exactly when they are needed. ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 \ PIP_DISABLE_PIP_VERSION_CHECK=1 # ── System libraries. One RUN, cleanup inside it, so the apt lists never ── # enter a layer. curl is needed by HEALTHCHECK. RUN apt-get update \ && apt-get install -y --no-install-recommends curl \ && rm -rf /var/lib/apt/lists/* # ── Non-root user. HF Spaces expect uid 1000. ──────────────────────────── RUN useradd -m -u 1000 user USER user ENV PATH="/home/user/.local/bin:$PATH" WORKDIR /home/user/app # ── THE CACHE BOUNDARY: manifest alone, then install. ──────────────────── COPY --chown=user requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # ── Volatile files last. Edits here never re-run the install above. ────── # superkart_features.py MUST be present: the pipeline's FunctionTransformer # references engineer() by module path, and joblib.load fails without it. COPY --chown=user superkart_features.py . COPY --chown=user superkart_model_v1.joblib . COPY --chown=user golden_sample.json model_manifest.json ./ COPY --chown=user app.py gunicorn.conf.py ./ # ── Contract with the platform ─────────────────────────────────────────── EXPOSE 7860 # start-period is generous: a 37 MB artefact must deserialize and pass the # golden-sample check before the first probe counts against the retry budget. HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \ CMD curl -fsS http://localhost:7860/health || exit 1 # ── Exec form: gunicorn is PID 1, so SIGTERM reaches it and shutdown is ── # graceful. Shell form would put /bin/sh at PID 1, which swallows signals. CMD ["gunicorn", "--config", "gunicorn.conf.py", "app:app"]