๐Ÿ“ฆ astral-sh / docs

๐Ÿ“„ index.md ยท 624 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624# [Using uv in Docker](#using-uv-in-docker)

## [Getting started](#getting-started)

Tip

Check out the [`uv-docker-example`](https://github.com/astral-sh/uv-docker-example) project for an example of best practices when using uv to build an application in Docker.

uv provides both *distroless* Docker images, which are useful for [copying uv binaries](#installing-uv) into your own image builds, and images derived from popular base images, which are useful for using uv in a container. The distroless images do not contain anything but the uv binaries. In contrast, the derived images include an operating system with uv pre-installed.

As an example, to run uv in a container using a Debian-based image:

```
$ docker run --rm -it ghcr.io/astral-sh/uv:debian uv --help

```

### [Available images](#available-images)

The following distroless images are available:

- `ghcr.io/astral-sh/uv:latest`
- `ghcr.io/astral-sh/uv:{major}.{minor}.{patch}`, e.g., `ghcr.io/astral-sh/uv:0.9.26`
- `ghcr.io/astral-sh/uv:{major}.{minor}`, e.g., `ghcr.io/astral-sh/uv:0.8` (the latest patch version)

And the following derived images are available:

- Based on `alpine:3.22`:
  - `ghcr.io/astral-sh/uv:alpine`
  - `ghcr.io/astral-sh/uv:alpine3.22`
- Based on `alpine:3.21`:
  - `ghcr.io/astral-sh/uv:alpine3.21`
- Based on `debian:trixie-slim`:
  - `ghcr.io/astral-sh/uv:debian-slim`
  - `ghcr.io/astral-sh/uv:trixie-slim`
- Based on `debian:bookworm-slim`:
  - `ghcr.io/astral-sh/uv:bookworm-slim`
- Based on `buildpack-deps:trixie`:
  - `ghcr.io/astral-sh/uv:debian`
  - `ghcr.io/astral-sh/uv:trixie`
- Based on `buildpack-deps:bookworm`:
  - `ghcr.io/astral-sh/uv:bookworm`
- Based on `python3.x-alpine`:
  - `ghcr.io/astral-sh/uv:python3.14-alpine`
  - `ghcr.io/astral-sh/uv:python3.13-alpine`
  - `ghcr.io/astral-sh/uv:python3.12-alpine`
  - `ghcr.io/astral-sh/uv:python3.11-alpine`
  - `ghcr.io/astral-sh/uv:python3.10-alpine`
  - `ghcr.io/astral-sh/uv:python3.9-alpine`
  - `ghcr.io/astral-sh/uv:python3.8-alpine`
- Based on `python3.x-trixie`:
  - `ghcr.io/astral-sh/uv:python3.14-trixie`
  - `ghcr.io/astral-sh/uv:python3.13-trixie`
  - `ghcr.io/astral-sh/uv:python3.12-trixie`
  - `ghcr.io/astral-sh/uv:python3.11-trixie`
  - `ghcr.io/astral-sh/uv:python3.10-trixie`
  - `ghcr.io/astral-sh/uv:python3.9-trixie`
- Based on `python3.x-slim-trixie`:
  - `ghcr.io/astral-sh/uv:python3.14-trixie-slim`
  - `ghcr.io/astral-sh/uv:python3.13-trixie-slim`
  - `ghcr.io/astral-sh/uv:python3.12-trixie-slim`
  - `ghcr.io/astral-sh/uv:python3.11-trixie-slim`
  - `ghcr.io/astral-sh/uv:python3.10-trixie-slim`
  - `ghcr.io/astral-sh/uv:python3.9-trixie-slim`
- Based on `python3.x-bookworm`:
  - `ghcr.io/astral-sh/uv:python3.14-bookworm`
  - `ghcr.io/astral-sh/uv:python3.13-bookworm`
  - `ghcr.io/astral-sh/uv:python3.12-bookworm`
  - `ghcr.io/astral-sh/uv:python3.11-bookworm`
  - `ghcr.io/astral-sh/uv:python3.10-bookworm`
  - `ghcr.io/astral-sh/uv:python3.9-bookworm`
  - `ghcr.io/astral-sh/uv:python3.8-bookworm`
- Based on `python3.x-slim-bookworm`:
  - `ghcr.io/astral-sh/uv:python3.14-bookworm-slim`
  - `ghcr.io/astral-sh/uv:python3.13-bookworm-slim`
  - `ghcr.io/astral-sh/uv:python3.12-bookworm-slim`
  - `ghcr.io/astral-sh/uv:python3.11-bookworm-slim`
  - `ghcr.io/astral-sh/uv:python3.10-bookworm-slim`
  - `ghcr.io/astral-sh/uv:python3.9-bookworm-slim`
  - `ghcr.io/astral-sh/uv:python3.8-bookworm-slim`

As with the distroless image, each derived image is published with uv version tags as `ghcr.io/astral-sh/uv:{major}.{minor}.{patch}-{base}` and `ghcr.io/astral-sh/uv:{major}.{minor}-{base}`, e.g., `ghcr.io/astral-sh/uv:0.9.26-alpine`.

In addition, starting with `0.8` each derived image also sets `UV_TOOL_BIN_DIR` to `/usr/local/bin` to allow `uv tool install` to work as expected with the default user.

For more details, see the [GitHub Container](https://github.com/astral-sh/uv/pkgs/container/uv) page.

### [Installing uv](#installing-uv)

Use one of the above images with uv pre-installed or install uv by copying the binary from the official distroless Docker image:

Dockerfile

```
FROM python:3.12-slim-trixie
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

```

Or, with the installer:

Dockerfile

```
FROM python:3.12-slim-trixie

# The installer requires curl (and certificates) to download the release archive
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates

# Download the latest installer
ADD https://astral.sh/uv/install.sh /uv-installer.sh

# Run the installer then remove it
RUN sh /uv-installer.sh && rm /uv-installer.sh

# Ensure the installed binary is on the `PATH`
ENV PATH="/root/.local/bin/:$PATH"

```

Note this requires `curl` to be available.

In either case, it is best practice to pin to a specific uv version, e.g., with:

```
COPY --from=ghcr.io/astral-sh/uv:0.9.26 /uv /uvx /bin/

```

Tip

While the Dockerfile example above pins to a specific tag, it's also possible to pin a specific SHA256. Pinning a specific SHA256 is considered best practice in environments that require reproducible builds as tags can be moved across different commit SHAs.

```
# e.g., using a hash from a previous release
COPY --from=ghcr.io/astral-sh/uv@sha256:2381d6aa60c326b71fd40023f921a0a3b8f91b14d5db6b90402e65a635053709 /uv /uvx /bin/

```

Or, with the installer:

```
ADD https://astral.sh/uv/0.9.26/install.sh /uv-installer.sh

```

### [Installing a project](#installing-a-project)

If you're using uv to manage your project, you can copy it into the image and install it:

Dockerfile

```
# Copy the project into the image
COPY . /app

# Disable development dependencies
ENV UV_NO_DEV=1

# Sync the project into a new environment, asserting the lockfile is up to date
WORKDIR /app
RUN uv sync --locked

```

Important

It is best practice to add `.venv` to a [`.dockerignore` file](https://docs.docker.com/build/concepts/context/#dockerignore-files) in your repository to prevent it from being included in image builds. The project virtual environment is dependent on your local platform and should be created from scratch in the image.

Then, to start your application by default:

Dockerfile

```
# Presuming there is a `my_app` command provided by the project
CMD ["uv", "run", "my_app"]

```

Tip

It is best practice to use [intermediate layers](#intermediate-layers) separating installation of dependencies and the project itself to improve Docker image build times.

See a complete example in the [`uv-docker-example` project](https://github.com/astral-sh/uv-docker-example/blob/main/Dockerfile).

### [Using the environment](#using-the-environment)

Once the project is installed, you can either *activate* the project virtual environment by placing its binary directory at the front of the path:

Dockerfile

```
ENV PATH="/app/.venv/bin:$PATH"

```

Or, you can use `uv run` for any commands that require the environment:

Dockerfile

```
RUN uv run some_script.py

```

Tip

Alternatively, the [`UV_PROJECT_ENVIRONMENT` setting](../../../concepts/projects/config/#project-environment-path) can be set before syncing to install to the system Python environment and skip environment activation entirely.

### [Using installed tools](#using-installed-tools)

To use installed tools, ensure the [tool bin directory](../../../concepts/tools/#tool-executables) is on the path:

Dockerfile

```
ENV PATH=/root/.local/bin:$PATH
RUN uv tool install cowsay

```

```
$ docker run -it $(docker build -q .) /bin/bash -c "cowsay -t hello"
  _____
| hello |
  =====
     \
      \
        ^__^
        (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||

```

Note

The tool bin directory's location can be determined by running the `uv tool dir --bin` command in the container.

Alternatively, it can be set to a constant location:

Dockerfile

```
ENV UV_TOOL_BIN_DIR=/opt/uv-bin/

```

## [Developing in a container](#developing-in-a-container)

When developing, it's useful to mount the project directory into a container. With this setup, changes to the project can be immediately reflected in a containerized service without rebuilding the image. However, it is important *not* to include the project virtual environment (`.venv`) in the mount, because the virtual environment is platform specific and the one built for the image should be kept.

### [Mounting the project with `docker run`](#mounting-the-project-with-docker-run)

Bind mount the project (in the working directory) to `/app` while retaining the `.venv` directory with an [anonymous volume](https://docs.docker.com/engine/storage/#volumes):

```
$ docker run --rm --volume .:/app --volume /app/.venv [...]

```

Tip

The `--rm` flag is included to ensure the container and anonymous volume are cleaned up when the container exits.

See a complete example in the [`uv-docker-example` project](https://github.com/astral-sh/uv-docker-example/blob/main/run.sh).

### [Configuring `watch` with `docker compose`](#configuring-watch-with-docker-compose)

When using Docker compose, more sophisticated tooling is available for container development. The [`watch`](https://docs.docker.com/compose/file-watch/#compose-watch-versus-bind-mounts) option allows for greater granularity than is practical with a bind mount and supports triggering updates to the containerized service when files change.

Note

This feature requires Compose 2.22.0 which is bundled with Docker Desktop 4.24.

Configure `watch` in your [Docker compose file](https://docs.docker.com/compose/compose-application-model/#the-compose-file) to mount the project directory without syncing the project virtual environment and to rebuild the image when the configuration changes:

compose.yaml

```
services:
  example:
    build: .

    # ...

    develop:
      # Create a `watch` configuration to update the app
      #
      watch:
        # Sync the working directory with the `/app` directory in the container
        - action: sync
          path: .
          target: /app
          # Exclude the project virtual environment
          ignore:
            - .venv/

        # Rebuild the image on changes to the `pyproject.toml`
        - action: rebuild
          path: ./pyproject.toml

```

Then, run `docker compose watch` to run the container with the development setup.

See a complete example in the [`uv-docker-example` project](https://github.com/astral-sh/uv-docker-example/blob/main/compose.yml).

## [Optimizations](#optimizations)

### [Compiling bytecode](#compiling-bytecode)

Compiling Python source files to bytecode is typically desirable for production images as it tends to improve startup time (at the cost of increased installation time and image size).

To enable bytecode compilation, use the `--compile-bytecode` flag:

Dockerfile

```
RUN uv python install --compile-bytecode
RUN uv sync --compile-bytecode

```

Alternatively, you can set the `UV_COMPILE_BYTECODE` environment variable to ensure that all commands within the Dockerfile compile bytecode:

Dockerfile

```
ENV UV_COMPILE_BYTECODE=1

```

Note

uv will only compile the standard library of *managed* Python versions during `uv python install`. The distributor of unmanaged Python versions decides if the standard library is pre-compiled. For example, the official `python` image will not have a compiled standard library.

### [Caching](#caching)

A [cache mount](https://docs.docker.com/build/guide/mounts/#add-a-cache-mount) can be used to improve performance across builds:

Dockerfile

```
ENV UV_LINK_MODE=copy

RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync

```

Changing the default [`UV_LINK_MODE`](../../../reference/settings/#link-mode) silences warnings about not being able to use hard links since the cache and sync target are on separate file systems.

If you're not mounting the cache, image size can be reduced by using the `--no-cache` flag or setting `UV_NO_CACHE`.

By default, managed Python installations are not cached before being installed. Setting `UV_PYTHON_CACHE_DIR` can be used in combination with a cache mount:

Dockerfile

```
ENV UV_PYTHON_CACHE_DIR=/root/.cache/uv/python

RUN --mount=type=cache,target=/root/.cache/uv \
    uv python install

```

Note

The cache directory's location can be determined by running the `uv cache dir` command in the container.

Alternatively, the cache can be set to a constant location:

Dockerfile

```
ENV UV_CACHE_DIR=/opt/uv-cache/

```

### [Intermediate layers](#intermediate-layers)

If you're using uv to manage your project, you can improve build times by moving your transitive dependency installation into its own layer via the `--no-install` options.

`uv sync --no-install-project` will install the dependencies of the project but not the project itself. Since the project changes frequently, but its dependencies are generally static, this can be a big time saver.

Dockerfile

```
# Install uv
FROM python:3.12-slim
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# Change the working directory to the `app` directory
WORKDIR /app

# Install dependencies
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 the project into the image
COPY . /app

# Sync the project
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --locked

```

Note that the `pyproject.toml` is required to identify the project root and name, but the project *contents* are not copied into the image until the final `uv sync` command.

Tip

If you want to remove additional, specific packages from the sync, use `--no-install-package <name>`.

#### [Intermediate layers in workspaces](#intermediate-layers-in-workspaces)

If you're using a [workspace](../../../concepts/projects/workspaces/), then a couple changes are needed:

- Use `--frozen` instead of `--locked` during the initially sync.
- Use the `--no-install-workspace` flag which excludes the project *and* any workspace members.

Dockerfile

```
# Install uv
FROM python:3.12-slim
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

WORKDIR /app

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 --frozen --no-install-workspace

COPY . /app

RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --locked

```

uv cannot assert that the `uv.lock` file is up-to-date without each of the workspace member `pyproject.toml` files, so we use `--frozen` instead of `--locked` to skip the check during the initial sync. The next sync, after all the workspace members have been copied, can still use `--locked` and will validate that the lockfile is correct for all workspace members.

### [Non-editable installs](#non-editable-installs)

By default, uv installs projects and workspace members in editable mode, such that changes to the source code are immediately reflected in the environment.

`uv sync` and `uv run` both accept a `--no-editable` flag, which instructs uv to install the project in non-editable mode, removing any dependency on the source code.

In the context of a multi-stage Docker image, `--no-editable` can be used to include the project in the synced virtual environment from one stage, then copy the virtual environment alone (and not the source code) into the final image.

For example:

Dockerfile

```
# Install uv
FROM python:3.12-slim AS builder
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

# Change the working directory to the `app` directory
WORKDIR /app

# Install dependencies
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-editable

# Copy the project into the intermediate image
COPY . /app

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

FROM python:3.12-slim

# Copy the environment, but not the source code
COPY --from=builder --chown=app:app /app/.venv /app/.venv

# Run the application
CMD ["/app/.venv/bin/hello"]

```

### [Using uv temporarily](#using-uv-temporarily)

If uv isn't needed in the final image, the binary can be mounted in each invocation:

Dockerfile

```
RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \
    uv sync

```

## [Using the pip interface](#using-the-pip-interface)

### [Installing a package](#installing-a-package)

The system Python environment is safe to use this context, since a container is already isolated. The `--system` flag can be used to install in the system environment:

Dockerfile

```
RUN uv pip install --system ruff

```

To use the system Python environment by default, set the `UV_SYSTEM_PYTHON` variable:

Dockerfile

```
ENV UV_SYSTEM_PYTHON=1

```

Alternatively, a virtual environment can be created and activated:

Dockerfile

```
RUN uv venv /opt/venv
# Use the virtual environment automatically
ENV VIRTUAL_ENV=/opt/venv
# Place entry points in the environment at the front of the path
ENV PATH="/opt/venv/bin:$PATH"

```

When using a virtual environment, the `--system` flag should be omitted from uv invocations:

Dockerfile

```
RUN uv pip install ruff

```

### [Installing requirements](#installing-requirements)

To install requirements files, copy them into the container:

Dockerfile

```
COPY requirements.txt .
RUN uv pip install -r requirements.txt

```

### [Installing a project](#installing-a-project_1)

When installing a project alongside requirements, it is best practice to separate copying the requirements from the rest of the source code. This allows the dependencies of the project (which do not change often) to be cached separately from the project itself (which changes very frequently).

Dockerfile

```
COPY pyproject.toml .
RUN uv pip install -r pyproject.toml
COPY . .
RUN uv pip install -e .

```

## [Verifying image provenance](#verifying-image-provenance)

The Docker images are signed during the build process to provide proof of their origin. These attestations can be used to verify that an image was produced from an official channel.

For example, you can verify the attestations with the [GitHub CLI tool `gh`](https://cli.github.com/):

```
$ gh attestation verify --owner astral-sh oci://ghcr.io/astral-sh/uv:latest
Loaded digest sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx for oci://ghcr.io/astral-sh/uv:latest
Loaded 1 attestation from GitHub API

The following policy criteria will be enforced:
- OIDC Issuer must match:................... https://token.actions.githubusercontent.com
- Source Repository Owner URI must match:... https://github.com/astral-sh
- Predicate type must match:................ https://slsa.dev/provenance/v1
- Subject Alternative Name must match regex: (?i)^https://github.com/astral-sh/

โœ“ Verification succeeded!

sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx was attested by:
REPO          PREDICATE_TYPE                  WORKFLOW
astral-sh/uv  https://slsa.dev/provenance/v1  .github/workflows/build-docker.yml@refs/heads/main

```

This tells you that the specific Docker image was built by the official uv GitHub release workflow and hasn't been tampered with since.

GitHub attestations build on the [sigstore.dev infrastructure](https://www.sigstore.dev/). As such you can also use the [`cosign` command](https://github.com/sigstore/cosign) to verify the attestation blob against the (multi-platform) manifest for `uv`:

```
$ REPO=astral-sh/uv
$ gh attestation download --repo $REPO oci://ghcr.io/${REPO}:latest
Wrote attestations to file sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.jsonl.
Any previous content has been overwritten

The trusted metadata is now available at sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.jsonl
$ docker buildx imagetools inspect ghcr.io/${REPO}:latest --format "{{json .Manifest}}" > manifest.json
$ cosign verify-blob-attestation \
    --new-bundle-format \
    --bundle "$(jq -r .digest manifest.json).jsonl"  \
    --certificate-oidc-issuer="https://token.actions.githubusercontent.com" \
    --certificate-identity-regexp="^https://github\.com/${REPO}/.*" \
    <(jq -j '.|del(.digest,.size)' manifest.json)
Verified OK

```

Tip

These examples use `latest`, but best practice is to verify the attestation for a specific version tag, e.g., `ghcr.io/astral-sh/uv:0.9.26`, or (even better) the specific image digest, such as `ghcr.io/astral-sh/uv:0.5.27@sha256:5adf09a5a526f380237408032a9308000d14d5947eafa687ad6c6a2476787b4f`.