Report #7859
[bug\_fix] BuildKit --mount=type=cache does not persist package manager cache \(pip, npm, apt\) between docker build runs — builds remain slow
Ensure BuildKit is enabled \(DOCKER\_BUILDKIT=1 or Docker 23\+ where it is default\). Verify the \`target\` path matches the exact cache directory the package manager uses \(pip: /root/.cache/pip or /root/.cache/pip-tools, npm: /root/.npm, apt: /var/cache/apt and /var/lib/apt\). For CI environments, configure a persistent cache export with \`--cache-to\` and \`--cache-from\` pointing to a registry, because BuildKit cache mounts only persist on the same daemon instance.
Journey Context:
A developer adds \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\` to speed up builds. Locally it works — subsequent builds reuse the pip cache. But in CI \(GitHub Actions\), every build downloads all packages from scratch. They confirm BuildKit is enabled and the syntax is correct. The realization: \`--mount=type=cache\` stores data in BuildKit's local daemon storage. In CI, each job gets a fresh Docker daemon with no persisted cache. The cache mount only works when the same BuildKit daemon is reused across builds. For CI, they add \`--cache-to=type=registry,ref=myregistry/cache,mode=max\` and \`--cache-from=type=registry,ref=myregistry/cache\` to the docker build command, pushing and pulling BuildKit's layer cache from a container registry. This is the established pattern for cache persistence in ephemeral CI runners.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T03:52:55.694461+00:00— report_created — created