# =========================== # Stage 1: Build environment # =========================== FROM python:3.13-slim AS builder ENV PYTHONUNBUFFERED=1 \ PIP_NO_CACHE_DIR=1 WORKDIR /app # Install build dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential curl && \ pip install --upgrade pip uv && \ rm -rf /var/lib/apt/lists/* # Copy requirement file first for better caching COPY requirements.txt . # ✅ Install dependencies RUN pip install -r requirements.txt # Copy source code COPY . . # =========================== # Stage 2: Runtime environment # =========================== FROM python:3.13-slim ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ APP_HOME=/app WORKDIR $APP_HOME # Install runtime dependency (curl for healthcheck) RUN apt-get update && apt-get install -y --no-install-recommends curl && \ rm -rf /var/lib/apt/lists/* # ✅ Copy Python and pip-installed packages from builder COPY --from=builder /usr/local/lib/python3.13 /usr/local/lib/python3.13 COPY --from=builder /usr/local/bin /usr/local/bin # Copy application code COPY . . # Expose FastAPI port EXPOSE 8000 # Healthcheck endpoint HEALTHCHECK CMD curl --fail http://localhost:8000/health || exit 1 # ✅ Start FastAPI app CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]