Originally published on kuryzhev.cloud
Adopting uv in CI images usually starts with a Dockerfile everyone has copied for years: python -m venv /opt/venv, pip install -r requirements.txt, and a cache layer that is invalidated by every dependency bump. The build works, but it is slow to rebuild, and nothing guarantees that two builds a week apart resolve the same transitive versions.
Short answer: if your CI installs Python dependencies on every run or image build, switch the install step to uv sync --locked against a committed uv.lock. Keep the old pip path available for one release cycle. Do it per repository, not fleet-wide in one go. Skip it if your builds depend on conda environments or on pip behavior you cannot yet express in pyproject.toml.
What changed and who is affected
uv, from Astral, is a single Rust binary that replaces several tools at once: pip, virtualenv/venv, pip-tools, and (optionally) pyenv-style interpreter management. It resolves, locks, and installs. The full reference is in the uv documentation.
For CI, three things change in practice:
-
The lockfile becomes the contract.
uv.lockpins every transitive dependency across platforms.requirements.txtbecomes an export, not the source of truth. -
The install step is a project sync.
uv synccreates.venvand installs exactly what the lock says. There is no separate venv-creation step. - Interpreters can come from uv. uv can download Python builds itself. This matters if your base image currently pins the interpreter. Python 3.15 is the current stable release, but check which minor version your project actually supports.
Affected: any team with Dockerfiles, GitHub Actions, GitLab CI or Jenkins agents that run pip install. Least affected: libraries that only run tests in a matrix. Most affected: monorepos with several requirements*.txt files and private package indexes.
Before/after config for uv in CI images
Here is the typical "before" and the replacement. The "after" is a multi-stage build that copies the uv binary from its official image, so the version is pinned explicitly.
# BEFORE: pip + venv
FROM python:3.15-slim
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# AFTER: uv + lockfile
ARG UV_VERSION=<pin-a-version-from-the-uv-releases-page>
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv
FROM python:3.15-slim
COPY --from=uv /uv /uvx /bin/
ENV UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
UV_PYTHON_DOWNLOADS=never \
PATH="/app/.venv/bin:$PATH"
WORKDIR /app
# Dependencies first: this layer only rebuilds when the lock changes
COPY pyproject.toml uv.lock ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-install-project --no-dev
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-dev
Here is what the key settings do:
-
--lockedfails the build ifuv.lockis out of date withpyproject.toml. That is the behavior you want in CI. -
UV_LINK_MODE=copyavoids hardlink warnings when the cache mount sits on a different filesystem from the target. -
UV_PYTHON_DOWNLOADS=neverforces uv to use the interpreter already in the image instead of fetching another one.
Also add a .dockerignore that excludes .venv. Otherwise COPY . . can copy a developer's local virtual environment over the one built in the image.
Step-by-step migration
Run these on a branch, one repository at a time.
1. Snapshot the current environment. This is your comparison baseline.
# inside the CURRENT CI image
pip freeze | sort > /tmp/before.txt
2. Import existing requirements into a project.
uv init --bare # creates pyproject.toml only
uv add -r requirements.txt # records deps in pyproject.toml
uv add --dev -r requirements-dev.txt # dev group; skip if you have none
uv python pin 3.15 # writes .python-version
uv lock # writes uv.lock
git add pyproject.toml uv.lock .python-version
If the repo is a library with its own build backend, add a [build-system] table first. Without one, uv treats it as a non-package project, which is fine for services.
3. Switch the install step. Replace the Dockerfile as shown above. In GitHub Actions:
- Use
actions/checkoutat its current major. - Use the
astral-sh/setup-uvaction, pinned to the major listed in its README. - Run
uv sync --lockedanduv run pytestinstead of activating a venv.
4. Add a lock-drift gate. Put uv lock --check in the lint stage. A pull request that edits pyproject.toml without updating the lock then fails early.
5. Replace activation with uv run. Anything in your pipeline that did source venv/bin/activate can become uv run <cmd>. Alternatively, rely on .venv/bin being on PATH in the image.
6. Trim the cache. For CI caches that persist between runs, finish the job with uv cache prune --ci. Per the documented CI guidance, it removes pre-built wheels and unpacked source distributions, which are cheap to re-download. It keeps wheels built from source, which are costly to rebuild.
Verify and roll back
Compare the new image to the baseline before merging.
# inside the NEW image
uv pip freeze | sort > /tmp/after.txt
diff /tmp/before.txt /tmp/after.txt # expect only intended version moves
uv pip check # expect: "All installed packages are compatible"
uv sync --locked --no-dev --dry-run # expect no changes planned
python -c "import sys; print(sys.prefix)" # expect a path ending in /app/.venv
Expect some differences in the diff:
-
Unpinned transitive dependencies. Versions can move where the old
requirements.txtdid not pin them. Review each one; this is where an unpinned package may have been drifting silently. - Package names. Names may also differ in case or separator style between the two tools. Normalize them before treating a line as a real change.
Then run the full test suite in the new image, not just an import check. Image build time and the size of the final layer in your registry are the most useful numbers to compare.
Rollback plan. Keep the old Dockerfile as Dockerfile.pip for one release cycle, and keep the previous image tag in the registry. If you must fall back without reverting the repository layout, export the lock:
uv export --format requirements-txt --no-dev -o requirements.txt
That yields a pinned, hash-checked requirements.txt that plain pip can install. The pip path keeps working during the transition.
Gotchas after the switch
Watch out for index resolution differences. pip with --extra-index-url considers all indexes together. uv's default strategy uses the first index that has the package. This protects against dependency confusion, but it breaks setups that relied on the old merge behavior. Define named indexes in pyproject.toml, and check the documented index strategy options before changing it.
Watch out for private index credentials. Credentials that lived in pip.conf or PIP_INDEX_URL do not carry over. You have two options:
- For a named index, use uv's per-index environment variables (
UV_INDEX_<NAME>_USERNAMEandUV_INDEX_<NAME>_PASSWORD). - Inject a
.netrcas a build secret.
Never bake a token into a layer. Confirm the exact variable names in the docs for your uv version.
More traps:
-
Venv path must match. If you copy
.venvbetween build stages, keep the same absolute path (/app/.venv) in both. Otherwise scripts with hardcoded shebangs break. -
Local venvs in the build context. Without a
.dockerignoreentry for.venv,COPY . .can clobber the image's environment. -
Non-root users. The cache mount targets
/root/.cache/uv. If the build runs as another user, change the mount target and setUV_CACHE_DIRto match. -
CUDA and platform wheels. Packages like PyTorch need an explicit index. Define it as a
[[tool.uv.index]]entry and map the package to it in[tool.uv.sources]. The default PyPI wheel may not be the variant you ship. -
Bot support. Confirm your dependency-update bot reads
uv.lockbefore you deleterequirements.txt. Otherwise updates will stop quietly.
Cost and when not to switch: you take on a new tool whose lock format is uv-specific, so moving away later means exporting. Conda-based images, or builds that rely on pip plugins and exotic resolver flags, are poor first candidates. For other CI tooling consolidation, see more runbooks on kuryzhev.cloud.
Top comments (0)