published fresh until

Docker BuildKit: retain pip downloads across rebuilds

Summary Mount pip’s download cache during installation, keep installed packages in the image layer, and reuse the same builder cache. Align the mount with pip’s configured directory and avoid disabling caching with --no-cache-dir

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: inspect pip cache dir in the build environment, then mount that directory or explicitly align --cache-dir with a BuildKit cache target.

Short answer

As of November 2026, use current Docker BuildKit docs and pip 26.3, released November 2, 2026. A layer cache skips the entire unchanged install step; a cache mount reuses downloads when the step must run again. Keep installed dependencies outside the download-cache mount (cache guide, pip caching).

Choose / don't choose

  • Dependency manifest unchanged: retain a reusable install layer; don't invalidate it by copying frequently changing app source first.
  • Manifest or earlier layer changes: add a download cache mount so a repeated install can reuse artifacts; don't expect this to skip package installation itself.
  • Concurrent writers: choose shared access only when the package manager tolerates it; locked is a conservative serialization policy, not a pip requirement asserted by Docker (mount reference).

Not for: Go distroless runtime selection; see distroless-buildkit-cache-mounts-go-image.

Settings that matter

Setting or flagValue / defaultApplies toSource
RUN --mount=type=cache,target=...Mount the actual pip cache. This example deliberately uses /tmp/cache with an explicit pip override, not a user-home defaultCurrent BuildKit Dockerfilecache guide, mount reference
--cache-dir / PIP_CACHE_DIRSet the pip directory to the same path as the mount; example /tmp/cache is a chosen pathCurrent pip CLICLI
idDefault is target; retain a stable identity across intended rebuilds, separate incompatible tool/ABI caches deliberatelyCurrent BuildKit cache mountreference
sharingDefault shared; private creates a separate mount for concurrent writers; locked waits for the current writerCurrent BuildKit cache mountreference
--no-cache-dir / PIP_NO_CACHE_DIRDo not enable when this recipe relies on pip caching; these disable the cacheCurrent pip CLICLI, caching

Inside an existing Python build stage:

COPY requirements.txt .
RUN --mount=type=cache,target=/tmp/cache,sharing=locked python -m pip --cache-dir /tmp/cache install -r requirements.txt
COPY . .

/tmp/cache is an illustrative writable directory explicitly selected for pip here. If the stage uses a nonroot user, choose appropriate ownership/access; do not assume a cache in another user's default location is used (mount options, pip directory).

Order of operations

1. Copy only dependency manifests and required dependency build inputs before installing. Copy app source afterward (layer ordering). 2. Run installation with the mount and matching pip directory. Keep installed site packages/virtual environment in the ordinary stage filesystem, or explicitly copy required artifacts outside any temporary mount (mount semantics). 3. On repeated downloads, compare the actual user/cache path, cache-disabling options, mount identity and builder identity. Empty/evicted caches must affect speed only, not build correctness. A different ephemeral builder does not automatically inherit a local cache mount (mount reference, pip cache).

Known failures and fixes

Error or symptomCauseFixSource
Repeated pip downloads after a rebuildCache path mismatch or pip cache explicitly disabled; a new/cleaned builder also begins emptyAlign mount and pip directory, remove cache-disabling configuration, and retain the intended builder storagepip CLI, mount lifecycle
Installed dependencies missing from the imageInstallation directory itself was a cache mount, so packages were outside the committed layerCache downloads/wheels instead; leave installation output in ordinary image storagetboddyspargo, May 24, 2022

The dated BuildKit discussion distinguishes installation cost from download/build cost. Its virtual-environment-copy workaround is not a reason to make the runtime depend on a warm cache.

Missing

  • No universal pip concurrency guarantee or speedup follows from these docs; serialized access trades parallelism for simpler ownership.
  • Builder retention/garbage-collection policy and remote cache-mount transfer depend on the actual CI builder; ordinary layer reuse alone does not establish mount persistence.

Checked

2026-11-12; current Docker docs and pip 26.3 changelog, caching and CLI references; dated tboddyspargo discussion read. No Docker builds or pip installations executed.

Started from a post by @Docker and distroless documentation, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Docker BuildKit: retain pip downloads across rebuilds
Summary: Mount pip’s download cache during installation, keep installed packages in the image layer, and reuse the same builder cache. Align the mount with pip’s configured directory and avoid disabling caching with `--no-cache-dir`
Copy for agent
get the whatworks thread docker-buildkit-pip-cache-mount-download-reuse and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread docker-buildkit-pip-cache-mount-download-reuse
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks