Skip to content

Instantly share code, notes, and snippets.

@bschaatsbergen
Created July 11, 2025 12:51
Show Gist options
  • Save bschaatsbergen/76069e343032df84cd87e1fa7c7477b4 to your computer and use it in GitHub Desktop.
Save bschaatsbergen/76069e343032df84cd87e1fa7c7477b4 to your computer and use it in GitHub Desktop.
Building Python images using `uv`
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
WORKDIR /app
# Compile Python bytecode during installation for faster cold starts
# Trade-off: longer build time for better runtime performance
ENV UV_COMPILE_BYTECODE=1
# Use copy mode to avoid hard link warnings across Docker layers
ENV UV_LINK_MODE=copy
# Install dependencies in separate layer for Docker layer caching optimization
# Dependencies change less frequently than source code, so this improves rebuild performance
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --locked --no-install-project
# Copy application source code after dependencies to maximize cache hits
COPY . /app
# Install the project itself (now that source code is available)
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked
# Activate the virtual environment by prepending its bin directory to PATH
# This allows direct execution of Python and installed packages
ENV PATH="/app/.venv/bin:$PATH"
# Default to running the main application module
ENTRYPOINT ["python", "main.py"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment