Skip to content

Instantly share code, notes, and snippets.

@MtkN1
Created March 26, 2024 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MtkN1/e07bf32865c64fecc4e5f1f0c8e4949c to your computer and use it in GitHub Desktop.
Save MtkN1/e07bf32865c64fecc4e5f1f0c8e4949c to your computer and use it in GitHub Desktop.
Dockerfile for uv
# build stage
FROM python:3.12-bookworm AS build
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
ENV PATH="/root/.cargo/bin:$PATH"
WORKDIR /app
COPY requirements.txt requirements.txt
RUN uv venv \
&& uv pip sync --no-cache requirements.txt
# runtime stage
FROM python:3.12-slim-bookworm AS runtime
WORKDIR /app
COPY --from=build /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH" \
VIRTUAL_ENV="/app/.venv"
COPY main.py /app
CMD [ "python", "-Bu", "main.py" ]
@MtkN1
Copy link
Author

MtkN1 commented Mar 26, 2024

This is a minimal sample of a Python container App using uv.

Thanks to uv, dependency installation and container building become extremely fast.

Workflows

  1. uv venv
  2. Add dependencies to pyproject.toml
  3. uv pip compile -o requirements.txt pyproject.toml
  4. uv pip compile --extra dev -o requirements-dev.txt pyproject.toml
  5. uv sync requirements-dev.txt
  6. Edit main.py
  7. docker build -t myapp .

Minimal pyproject.toml

[project]
name = "myapp"
version = "0.1.0"
dependencies = ["aiohttp", "google-cloud-bigquery"]

[project.optional-dependencies]
dev = ["pytest", "pytest-aiohttp", "ruff"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment