# ============================================================================= # FASHIONISTAR — API Gateway Dockerfile (Hugging Face Spaces — v1) # ============================================================================= # Target: huggingface.co/spaces/fashionistar-ai/fashionistar-api-v1 # Port: 7860 (HF mandatory, Django ASGI via Gunicorn + Uvicorn workers) # UID: 1000 (HF mandatory) # # Architecture mirrors the working Celery Dockerfiles exactly. # Key differences from the old v2: # ✓ Ollama removed — runs in fashionistar-ai-engine (ZeroGPU space) # ✓ dos2unix included to strip any Windows CRLF from entrypoint.sh # ✓ No ARG instructions (HF injects secrets as build ARGs → breaks FROM) # ✓ Non-fatal migrations (DB SSL errors won't crash gunicorn) # # HF Spaces Dev Mode requirements (PRO plan): # ✓ bash, git, git-lfs, curl, wget, procps installed # ✓ /app owned by UID 1000 # ✓ CMD instruction present # ============================================================================= # ── Stage 1: uv binary ─────────────────────────────────────────────────────── # Versions are hardcoded (NOT ARG) because HF Spaces injects Space secrets as # Docker build args, overriding ARG defaults and causing build failures. FROM ghcr.io/astral-sh/uv:0.8.22 AS uvbin # ── Stage 2: dependency builder ────────────────────────────────────────────── FROM python:3.13-slim AS builder ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ UV_COMPILE_BYTECODE=1 \ UV_LINK_MODE=copy \ UV_PROJECT_ENVIRONMENT=/opt/venv \ VIRTUAL_ENV=/opt/venv RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ libpq-dev \ && rm -rf /var/lib/apt/lists/* COPY --from=uvbin /uv /uvx /bin/ WORKDIR /app COPY pyproject.toml uv.lock ./ # Install dependencies, strip heavy ML/GPU packages (those go in AI engine space) RUN uv sync --frozen --no-dev --no-editable --no-install-project && \ uv pip uninstall --python /opt/venv \ torch torchvision mediapipe open-clip-torch sentence-transformers \ opencv-python-headless opencv-contrib-python \ nvidia-cuda-nvrtc-cu12 nvidia-cuda-runtime-cu12 nvidia-cuda-cupti-cu12 \ nvidia-cudnn-cu12 nvidia-cublas-cu12 nvidia-cufft-cu12 nvidia-curand-cu12 \ nvidia-cusolver-cu12 nvidia-cusparse-cu12 nvidia-nccl-cu12 \ nvidia-nvtx-cu12 nvidia-nvjitlink-cu12 triton 2>/dev/null || true # ── Stage 3: production runtime ─────────────────────────────────────────────── FROM python:3.13-slim AS runner ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ UV_LINK_MODE=copy \ UV_PROJECT_ENVIRONMENT=/opt/venv \ PATH="/opt/venv/bin:$PATH" \ DJANGO_SETTINGS_MODULE=backend.config.production \ WORKING_ENVIRONMENT=production \ PORT=7860 \ OLLAMA_ENABLED=False \ UVICORN_WS=auto \ GUNICORN_WORKERS=3 \ GUNICORN_TIMEOUT=120 \ GUNICORN_KEEPALIVE=75 \ GUNICORN_MAX_REQUESTS=1000 \ GUNICORN_MAX_REQUESTS_JITTER=100 \ GUNICORN_LOG_LEVEL=info # Runtime system dependencies # IMPORTANT: bash, git, git-lfs, wget, procps are REQUIRED for HF Spaces Dev Mode (PRO plan). # dos2unix strips Windows CRLF (\\r\\n) from entrypoint.sh (safety net for Windows devs). # libpq5 for psycopg runtime. postgresql-client for manage.py dbshell debugging. RUN apt-get update && apt-get install -y --no-install-recommends \ libpq5 \ postgresql-client \ curl \ wget \ zstd \ bash \ dos2unix \ git \ git-lfs \ procps \ && git lfs install \ && rm -rf /var/lib/apt/lists/* COPY --from=uvbin /uv /uvx /bin/ COPY --from=builder /opt/venv /opt/venv # Create non-root app user (UID 1000 — MANDATORY for HF Spaces) RUN useradd --create-home --uid 1000 --shell /bin/bash appuser && \ mkdir -p /app/staticfiles /app/media /app/logs && \ chown -R appuser:appuser /app /home/appuser WORKDIR /app # Copy application source (after deps — maximizes layer cache hits) COPY --chown=appuser:appuser . . # Copy entrypoint as root (must be before USER switch to set ownership + strip CRLF) COPY --chown=appuser:appuser entrypoint.sh /entrypoint.sh RUN dos2unix /entrypoint.sh && chmod +x /entrypoint.sh USER appuser # Port 7860 — mandatory for HF Spaces EXPOSE 7860 # Health check — polls the lightweight health endpoint HEALTHCHECK --interval=30s --timeout=15s --start-period=120s --retries=3 \ CMD curl -fsS "http://127.0.0.1:${PORT:-7860}/api/v1/health/" || exit 1 ENTRYPOINT ["/entrypoint.sh"] # Default: start the Django ASGI API via Gunicorn + Uvicorn workers. # entrypoint.sh handles: migrations (non-fatal), collectstatic, then gunicorn. CMD ["api"]