Spaces:
Sleeping
Sleeping
| # Multi-stage Dockerfile for FastAPI with uv | |
| # Stage 1: Builder - Install dependencies with uv | |
| FROM python:3.12-slim AS builder | |
| # Install uv | |
| COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv | |
| # Set working directory | |
| WORKDIR /app | |
| # Set environment variables for uv and Python | |
| ENV UV_COMPILE_BYTECODE=1 | |
| ENV UV_LINK_MODE=copy | |
| ENV PYTHONDONTWRITEBYTECODE=1 | |
| ENV PYTHONUNBUFFERED=1 | |
| # Copy dependency files | |
| COPY requirements.txt ./ | |
| # Create virtual environment and install dependencies | |
| RUN uv venv /opt/venv && \ | |
| . /opt/venv/bin/activate && \ | |
| uv pip install --no-cache -r requirements.txt | |
| # Stage 2: Runner - Production image | |
| FROM python:3.12-slim AS runner | |
| # Set working directory | |
| WORKDIR /app | |
| # Create non-root user for security | |
| RUN groupadd --gid 1001 appuser && \ | |
| useradd --uid 1001 --gid 1001 --shell /bin/bash --create-home appuser | |
| # Set environment variables | |
| ENV PYTHONDONTWRITEBYTECODE=1 | |
| ENV PYTHONUNBUFFERED=1 | |
| ENV PATH="/opt/venv/bin:$PATH" | |
| ENV PYTHONPATH=/app | |
| # Copy virtual environment from builder | |
| COPY --from=builder /opt/venv /opt/venv | |
| # Copy application code | |
| COPY --chown=appuser:appuser . . | |
| # Switch to non-root user | |
| USER appuser | |
| # Expose port | |
| EXPOSE 7860 | |
| # Health check | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ | |
| CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/health').read()" || exit 1 | |
| # Start the application with uvicorn | |
| CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "4"] | |