Spaces:
Sleeping
Sleeping
File size: 1,529 Bytes
3bb9eac cdaf146 3bb9eac cdaf146 3bb9eac cdaf146 3bb9eac cdaf146 3bb9eac a32362d 3bb9eac cdaf146 3bb9eac a32362d 3bb9eac a900820 3bb9eac a900820 cdaf146 3bb9eac a900820 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | # 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"]
|