# 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 November 30, 2026. Canonical: https://sniptide.com/templates/python-dockerfile
Open it in the Sniptide editor: https://app.sniptide.com/new?template=python-dockerfile
Files as JSON: https://sniptide.com/templates/python-dockerfile.json

## Files

### Dockerfile

```dockerfile
# 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"]
```

### .dockerignore

```
.git
.venv
__pycache__
*.pyc
.pytest_cache
.ruff_cache
.mypy_cache
.env
.env.*
!.env.example
Dockerfile
.dockerignore
compose.yaml
tests
```

## Build and run it [#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.

```bash title="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 [#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 [#variant-with-pip-and-requirementstxt]

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

```dockerfile title="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 [#variant-run-it-with-a-database]

Add the image to a Compose file next to [Postgres](/templates/docker-compose-postgres) or [Redis](/templates/docker-compose-redis), and build it on every push with the [GitHub Actions Docker workflow](/templates/github-actions-docker).
