Python & FastAPI•Part 1: Candid Experience, Architecture & Intro (1 - 5)
Strategy for dependency management and packaging:
In Simple Words: I use uv (Astral's blazingly fast Rust-based package manager) or Poetry with deterministic lockfiles. In Docker, I use Multi-Stage Builds:
- Builder stage: Compiles C extensions and wheels.
- Final stage: Uses a slim image (
python:3.12-slim), copies only installed packages, and runs as a non-root user (USER nonroot).
dockerfile
# Multi-stage Dockerfile using uv for ultra-fast builds:
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-install-project
FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"
USER 10001
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]