Skip to content
Sniptide

Python Dockerfile with uv

This Dockerfile installs your dependencies with uv from uv.lock in a build stage, then copies the virtual environment into a slim image that runs as a non-root user. Save it with the .dockerignore next to pyproject.toml, change the last line to start your app, and run docker build.

Updated

Files

DockerfileRaw
# syntax=docker/dockerfile:1

# Build stage: install dependencies into a virtual environment with uv.
FROM python:3.13-slim AS build

COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# Compile bytecode for faster starts, and copy files out of the cache instead of linking them.
ENV UV_COMPILE_BYTECODE=1 \
    UV_LINK_MODE=copy \
    UV_PYTHON_DOWNLOADS=0

WORKDIR /app

# Dependencies first, so this layer stays cached until pyproject.toml or uv.lock changes.
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 --no-dev

# Then the project itself.
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --locked --no-dev --no-editable

# Runtime stage: the same Python, the virtual environment and the code, without uv.
FROM python:3.13-slim

RUN groupadd --system app && useradd --system --gid app --no-create-home app

WORKDIR /app
COPY --from=build --chown=app:app /app /app

ENV PATH="/app/.venv/bin:$PATH" \
    PYTHONUNBUFFERED=1

USER app
EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
.dockerignoreRaw
.git
.venv
__pycache__
*.pyc
.pytest_cache
.ruff_cache
.mypy_cache
.env
.env.*
!.env.example
Dockerfile
.dockerignore
compose.yaml
tests

Save with your agent

Run this command to add Sniptide to Claude Code in the current project. Open /mcp in Claude Code and sign in to Sniptide. Once the server shows as connected, use the prompt below to save a private copy in your library.

Terminal
claude mcp add --scope local --transport http sniptide https://app.sniptide.com/api/mcp
Prompt
Read https://sniptide.com/templates/python-dockerfile.json and use create_snippet to save its files as a private Sniptide snippet titled "Python Dockerfile with uv". Keep the file names and contents from the JSON.

Build and run it

Use Docker with BuildKit, which is the default in current Docker releases. The project needs a pyproject.toml and a committed uv.lock, created with uv lock. The example starts a FastAPI app at app/main.py with Uvicorn, so uvicorn must be a dependency.

Terminal
docker build -t my-api .
docker run --rm -p 127.0.0.1:8000:8000 my-api

Open http://localhost:8000/docs to check that the app answers. Press Ctrl+C to stop the container. For a Django or Flask app, replace the CMD with your server, such as ["gunicorn", "mysite.wsgi", "--bind", "0.0.0.0:8000"].

What each stage does

Part
What it does
When to change it
FROM python:3.13-slim
The Debian-based slim image. It has the C libraries most wheels expect, unlike Alpine.
When you move to another Python version. Use the same tag in both stages.
COPY --from=ghcr.io/astral-sh/uv
Copies the uv binary from its official image, so nothing else is installed.
Replace latest with the uv version you use locally (uv --version) for repeatable builds.
First uv sync
Installs only the dependencies, using bind mounts for the two files it needs. Changing your code doesn't invalidate this layer.
Rarely.
--mount=type=cache
Keeps uv's download cache between builds on the same machine.
Rarely.
Second uv sync
Installs your project into the virtual environment. --no-editable copies it in, so the runtime stage doesn't depend on the source layout.
Rarely.
Runtime stage
Starts from a clean slim image and copies /app, including .venv. uv and the build cache stay behind.
Add system packages your app needs at runtime here, such as libpq5.
USER app
Runs the process without root.
Never remove it.

--locked fails the build when uv.lock doesn't match pyproject.toml, so the image always has the versions you tested. --no-dev leaves out test and lint tools.

The .dockerignore keeps .venv, caches and .env files out of the build context. Your local virtual environment would otherwise overwrite the one built in the image.

Variant: with pip and requirements.txt

For a project without uv, the same structure works with a virtual environment and pip:

Dockerfile
# syntax=docker/dockerfile:1
FROM python:3.13-slim AS build
WORKDIR /app
RUN python -m venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"
COPY requirements.txt .
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
COPY . .

FROM python:3.13-slim
RUN groupadd --system app && useradd --system --gid app --no-create-home app
WORKDIR /app
COPY --from=build --chown=app:app /app /app
ENV PATH="/app/.venv/bin:$PATH" PYTHONUNBUFFERED=1
USER app
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

Variant: run it with a database

Add the image to a Compose file next to Postgres or Redis, and build it on every push with the GitHub Actions Docker workflow.

All templates
  • Docker and deployDocker Compose Postgres

    A Docker Compose file for Postgres with a persistent volume, a health check and optional init scripts, plus variants with an app service and pgAdmin.

  • Docker and deployDocker Compose Redis

    A Docker Compose file for Redis with a password, persistence, a memory limit and a health check, plus variants with an app service and Redis Insight.

  • Docker and deployGitHub Actions Docker build and push

    A GitHub Actions workflow that builds a Docker image on pull requests and pushes it to GitHub Container Registry from main and version tags, with caching.

Pick where your library lives.

Use sniptide.com

Save your first pattern in the app, then connect Claude Code, Cursor or Codex with one sign-in in the browser.

Run it on your own server

One image and a database give you the app, the API and the MCP endpoint. Sniptide is open source under AGPL-3.0.

$docker compose up -d