30 Python packages with uv in an image
Learning Objectives
- Install a uv project’s Python packages into a Docker image with
uv sync --locked - Explain why the image keeps its virtual environment in
/opt/venv, outside the mounted project folder - Add a Python package to a project whose packages are in an image, and fix a
uv.lockthat no longer matchespyproject.toml
Download the images before you start
The examples in this chapter build on two images. Start Docker first (on macOS and Windows, open Docker Desktop), then download (pull) them before you start:
Terminal
docker pull ubuntu:24.04
docker pull ghcr.io/astral-sh/uv:0.13.730.1 The project
This chapter puts the Python packages of a small uv project into a Docker image. It is a uv project like the ones in Virtual environments: Python (uv), and its files are in the book repository’s demos/docker-uv/end folder. hello.py prints a message with the cowsay package:
import cowsay
cowsay.cow("Hello from Python in my image!")pyproject.toml lists the package with an exact version, and add-bounds = "exact" makes uv add write exact versions too (see uv: pinning exact versions):
[project]
name = "docker-uv"
version = "0.1.0"
requires-python = ">=3.14"
dependencies = [
"cowsay==6.1",
]
[tool.uv]
# `uv add` writes an exact version (==)
add-bounds = "exact".python-version holds 3.14, and uv.lock locks cowsay and everything it needs. On your computer, uv run creates the project’s .venv folder and runs the script:
Terminal
uv run hello.pyOutput
Using CPython 3.14.3
Creating virtual environment at: .venv
Installed 1 package in 9ms
______________________________
| Hello from Python in my image! |
==============================
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
30.2 A Dockerfile for a uv project
Here is the project’s Dockerfile. Its uv lines are the same as in docker-uv-renv, the example project that Docker Compose uses:
# Ubuntu Linux, with no Python: uv installs Python itself
FROM ubuntu:24.04
# install python uv
# https://docs.astral.sh/uv/guides/integration/docker/#installing-uv
COPY --from=ghcr.io/astral-sh/uv:0.13.7 /uv /uvx /usr/local/bin/
# The project is mounted over /workspace, so the packages go in /opt
WORKDIR /workspace
# python packages in /opt/venv
ENV UV_PROJECT_ENVIRONMENT=/opt/venv
COPY pyproject.toml uv.lock .python-version ./
# --locked: stop if uv.lock doesn't match pyproject.toml
RUN uv sync --lockedFROM ubuntu:24.04starts from Ubuntu Linux, which has no Python. uv installs the Python version that.python-versionasks for.COPY --from=ghcr.io/astral-sh/uv:0.13.7copies theuvanduvxprograms out of the image that uv publishes, as uv’s Docker guide shows. The tag pins uv’s version, the same way a lockfile pins a package’s.WORKDIR /workspacemakes/workspacethe working directory, for the rest of the build and for containers that run from the image. You will mount your project folder there.ENV UV_PROJECT_ENVIRONMENT=/opt/venvtells uv to put the project’s virtual environment in/opt/venv, instead of in.venvinside the project folder (see uv’s environment variables). The section after next shows why.COPY pyproject.toml uv.lock .python-version ./copies only the files that list the packages, not the code. Editinghello.pythen never reinstalls the packages, because theCOPYandRUNsteps above it have not changed (see Building images).RUN uv sync --lockedinstalls Python and the packages listed inuv.lock.--lockedstops the build ifuv.lockdoes not matchpyproject.toml.
30.3 Build the image
From the project folder:
Terminal
docker build --tag docker-uv .Output
[+] Building 8.8s (12/12) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 553B 0.0s
=> [internal] load metadata for ghcr.io/astral-sh/uv:0.13.7 0.6s
=> [internal] load metadata for docker.io/library/ubuntu:24.04 0.1s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CACHED FROM ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e 0.2s
=> => resolve ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1 0.1s
=> CACHED [stage-0 1/5] FROM docker.io/library/ubuntu:24.04@sha256:008173c23f95b170204355c12 0.2s
=> => resolve docker.io/library/ubuntu:24.04@sha256:008173c23f95b170204355c12626cb5a965d779a 0.2s
=> [internal] load build context 0.0s
=> => transferring context: 964B 0.0s
=> [stage-0 2/5] COPY --from=ghcr.io/astral-sh/uv:0.13.7 /uv /uvx /usr/local/bin/ 0.1s
=> [stage-0 3/5] WORKDIR /workspace 0.0s
=> [stage-0 4/5] COPY pyproject.toml uv.lock .python-version ./ 0.0s
=> [stage-0 5/5] RUN uv sync --locked 3.7s
=> exporting to image 3.9s
=> => exporting layers 2.8s
=> => exporting manifest sha256:6e630dbe112a1dc156ddc9fd6ad3c7b8585f9c045fdb5c95546843ec6ca2 0.0s
=> => exporting config sha256:bbae306c5d2d7852ecf4b128ed8508269a04e620187bc9e8a5fd9a5d68cfe3 0.0s
=> => exporting attestation manifest sha256:6e9b8ca9f5749e9b646d18487a8066fbb2a7ac07ae4971d5 0.0s
=> => exporting manifest list sha256:c7eced3c63cca5bff431f82a2ec381a1bc17a49a2839cd91ad1b5c0 0.0s
=> => naming to docker.io/library/docker-uv:latest 0.0s
=> => unpacking to docker.io/library/docker-uv:latest 1.0s
The first FROM line is uv’s image, which COPY --from reads uv from. To see what uv sync printed, build with --progress=plain, which shows each step’s own output, and --no-cache, which runs every step again instead of reusing its layer. Here is the uv sync step:
Terminal
docker build --progress=plain --no-cache --tag docker-uv .Output
#11 [stage-0 5/5] RUN uv sync --locked
#11 0.309 Downloading cpython-3.14.10-linux-aarch64-gnu (download) (29.2MiB)
#11 3.201 Downloaded cpython-3.14.10-linux-aarch64-gnu (download)
#11 3.352 Using CPython 3.14.10
#11 3.352 Creating virtual environment at: /opt/venv
#11 3.359 Resolved 2 packages in 2ms
#11 3.434 Prepared 1 package in 74ms
#11 3.451 Installed 1 package in 16ms
uv downloaded Python 3.14, created the virtual environment at /opt/venv, and installed cowsay into it.
30.4 Run the project in a container
The image has the packages but not the code. Mount the project folder at /workspace, and run the script with uv run in place of the image’s default command (see Running containers):
Terminal
docker run --rm -v "$(pwd):/workspace" docker-uv uv run hello.pyTerminal
docker run --rm -v "/$(pwd):/workspace" docker-uv uv run hello.pydocker run --rm -v "${PWD}:/workspace" docker-uv uv run hello.pyOutput
______________________________
| Hello from Python in my image! |
==============================
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
/$(pwd) in Git Bash?
Git Bash turns command-line arguments that start with / into Windows paths, so /workspace could reach Docker as C:/Program Files/Git/workspace. Doubling the first slash, which /$(pwd) does, stops that (see “Known issues” in the Git for Windows release notes).
30.5 Why the packages go in /opt/venv
Without a mount, /workspace holds the three files the Dockerfile copied:
Terminal
docker run --rm docker-uv ls -aOutput
.
..
.python-version
pyproject.toml
uv.lock
With the project folder mounted, /workspace shows your computer’s folder instead, including the .venv folder that uv run created on your computer:
Terminal
docker run --rm -v "$(pwd):/workspace" docker-uv ls -aTerminal
docker run --rm -v "/$(pwd):/workspace" docker-uv ls -adocker run --rm -v "${PWD}:/workspace" docker-uv ls -aOutput
.
..
.gitignore
.python-version
.venv
Dockerfile
hello.py
pyproject.toml
uv.lock
A mount hides whatever the image had at that path. If the image kept its packages in /workspace/.venv, the mount would hide them. Worse, uv in the container would find your computer’s .venv instead, which holds a program for your computer’s operating system, not for Linux. On a Mac:
Terminal
file .venv/bin/python3Output
.venv/bin/python3: Mach-O 64-bit executable arm64
With the ENV line left out of the Dockerfile, running uv run hello.py with the mount printed these lines, among others:
Output
Removed virtual environment at: .venv
Creating virtual environment at: .venv
uv deleted your computer’s .venv and made a Linux one in its place, which no longer works on your computer. With UV_PROJECT_ENVIRONMENT=/opt/venv, the container leaves .venv alone and uses the environment that the image built:
Terminal
docker run --rm -v "$(pwd):/workspace" docker-uv uv run python -c "import sys; print(sys.prefix)"Terminal
docker run --rm -v "/$(pwd):/workspace" docker-uv uv run python -c "import sys; print(sys.prefix)"docker run --rm -v "${PWD}:/workspace" docker-uv uv run python -c "import sys; print(sys.prefix)"Output
/opt/venv
30.6 Add a package
To add a package, use uv on your computer, as in uv: projects, then build the image again. For example, to add pandas:
Terminal
uv add pandasOutput
Resolved 6 packages in 26ms
Installed 4 packages in 100ms
+ numpy==2.5.5
+ pandas==3.1.1
+ python-dateutil==4.0.0
+ tzdata==2026.6
Because of add-bounds = "exact", uv add wrote pandas into pyproject.toml with its exact version:
dependencies = [
"cowsay==6.1",
"pandas==3.1.1",
]It also updated uv.lock with pandas and the packages pandas needs. Build the image again:
Terminal
docker build --tag docker-uv .Here are the steps from the output:
Output
[+] Building 16.5s (12/12) FINISHED docker:desktop-linux
=> [stage-0 1/5] FROM docker.io/library/ubuntu:24.04@sha256:008173c23f95b170204355c12626cb5a 0.0s
=> CACHED [stage-0 2/5] COPY --from=ghcr.io/astral-sh/uv:0.13.7 /uv /uvx /usr/local/bin/ 0.0s
=> CACHED [stage-0 3/5] WORKDIR /workspace 0.0s
=> [stage-0 4/5] COPY pyproject.toml uv.lock .python-version ./ 0.0s
=> [stage-0 5/5] RUN uv sync --locked 7.4s
Docker reused the layers with uv in them, and ran the COPY and uv sync steps again, because pyproject.toml and uv.lock changed. pandas is now in the image:
Terminal
docker run --rm -v "$(pwd):/workspace" docker-uv uv run python -c "import pandas; print(pandas.__version__)"Terminal
docker run --rm -v "/$(pwd):/workspace" docker-uv uv run python -c "import pandas; print(pandas.__version__)"docker run --rm -v "${PWD}:/workspace" docker-uv uv run python -c "import pandas; print(pandas.__version__)"Output
3.1.1
Commit pyproject.toml and uv.lock together, so that the lockfile in your repository always matches pyproject.toml.
30.7 When uv.lock does not match pyproject.toml
If you edit pyproject.toml by hand, without uv add or uv lock, uv.lock no longer matches it. For example, typing the pandas line into pyproject.toml yourself gives the same dependencies as uv add pandas did, but leaves uv.lock without pandas:
dependencies = [
"cowsay==6.1",
"pandas==3.1.1",
]The build then stops at uv sync --locked:
Terminal
docker build --tag docker-uv .Output
[+] Building 5.2s (11/11) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 553B 0.0s
=> [internal] load metadata for ghcr.io/astral-sh/uv:0.13.7 0.8s
=> [internal] load metadata for docker.io/library/ubuntu:24.04 0.1s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> FROM ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1334471 0.0s
=> => resolve ghcr.io/astral-sh/uv:0.13.7@sha256:96f40d3904aad8d0bd010539001d36c8e2188b981e1 0.0s
=> [stage-0 1/5] FROM docker.io/library/ubuntu:24.04@sha256:008173c23f95b170204355c12626cb5a 0.0s
=> => resolve docker.io/library/ubuntu:24.04@sha256:008173c23f95b170204355c12626cb5a965d779a 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 985B 0.0s
=> CACHED [stage-0 2/5] COPY --from=ghcr.io/astral-sh/uv:0.13.7 /uv /uvx /usr/local/bin/ 0.0s
=> CACHED [stage-0 3/5] WORKDIR /workspace 0.0s
=> [stage-0 4/5] COPY pyproject.toml uv.lock .python-version ./ 0.0s
=> ERROR [stage-0 5/5] RUN uv sync --locked 4.3s
------
> [stage-0 5/5] RUN uv sync --locked:
0.287 Downloading cpython-3.14.10-linux-aarch64-gnu (download) (29.2MiB)
3.631 Downloaded cpython-3.14.10-linux-aarch64-gnu (download)
3.817 Using CPython 3.14.10
3.817 Creating virtual environment at: /opt/venv
4.141 Resolved 6 packages in 319ms
4.143 error: The lockfile at `uv.lock` needs to be updated, but `--locked` was provided.
4.143
4.143 hint: To update the lockfile, run `uv lock`.
------
Dockerfile:15
--------------------
13 | COPY pyproject.toml uv.lock .python-version ./
14 | # --locked: stop if uv.lock doesn't match pyproject.toml
15 | >>> RUN uv sync --locked
16 |
--------------------
ERROR: failed to build: failed to solve: process "/bin/sh -c uv sync --locked" did not complete successfully: exit code: 1
What's next:
Debug this build failure with Gordon → docker ai "help me fix this build failure"
This is what --locked is for. Without it, uv sync would quietly update uv.lock inside the image, and the image would no longer match the uv.lock in your repository. With it, a forgotten uv lock stops the build, and the error says how to fix it:
Terminal
uv lockOutput
Using CPython 3.14.3
Resolved 6 packages in 49ms
Added numpy v2.5.5
Added pandas v3.1.1
Added python-dateutil v4.0.0
Added tzdata v2026.6
After uv lock, docker build --tag docker-uv . finishes, and pyproject.toml and uv.lock are ready to commit together.