Skip to content

Instantly share code, notes, and snippets.

@michaelkosir
Last active March 6, 2025 05:56
Show Gist options
  • Save michaelkosir/741352e18345dc7a5fadfc8b5d735ce3 to your computer and use it in GitHub Desktop.
Save michaelkosir/741352e18345dc7a5fadfc8b5d735ce3 to your computer and use it in GitHub Desktop.
An example Python web application distroless image using uv. An extremely fast Python package and project manager, written in Rust.
# Stage 0
# Installs dependencies and builds the application
# Artifacts will be copied to the final image
FROM debian:12-slim AS build
ARG PYTHON_VERSION="3.13"
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin
ENV UV_COMPILE_BYTECODE="1"
ENV UV_LINK_MODE="copy"
ENV UV_PYTHON_INSTALL_DIR="/python"
ENV UV_PYTHON_PREFERENCE="only-managed"
WORKDIR /app
RUN uv python install $PYTHON_VERSION --no-cache
RUN --mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-cache --no-dev --no-editable
COPY src /app/src
# Stage 1
# Uses GoogleContainerTools/distroless as a minimal base
# Contains only necessary files and build artifacts
# Caching improves build speed
FROM gcr.io/distroless/cc-debian12:nonroot
COPY --from=build /python /python
COPY --from=build /app /app
ENV PATH="/app/.venv/bin:$PATH"
WORKDIR /app
ENTRYPOINT ["uvicorn", "src.main:app"]
CMD ["--host=0.0.0.0", "--port=8000"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment