| # Multi-stage build for efficiency | |
| FROM python:3.11-slim as builder | |
| # Install build dependencies | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| build-essential \ | |
| && rm -rf /var/lib/apt/lists/* | |
| WORKDIR /tmp | |
| COPY requirements.txt . | |
| RUN pip install --user --no-cache-dir -r requirements.txt | |
| # Final stage | |
| FROM python:3.11-slim | |
| # Create app user (required for HF Spaces security) | |
| RUN useradd -m -u 1000 user | |
| WORKDIR /app | |
| # Copy Python packages from builder | |
| COPY --from=builder /root/.local /home/user/.local | |
| # Copy application code | |
| COPY --chown=user:user . /app/ | |
| # Set environment | |
| ENV PATH=/home/user/.local/bin:$PATH \ | |
| PYTHONUNBUFFERED=1 \ | |
| PYTHONDONTWRITEBYTECODE=1 | |
| # Switch to non-root user | |
| USER user | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \ | |
| CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/').read()" || exit 1 | |
| EXPOSE 7860 | |
| CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"] | |