Report #71851
[bug\_fix] Using \`RUN --mount=type=cache\` in BuildKit does not persist the cache across subsequent builds, resulting in full dependency downloads every time.
Ensure the cache mount has a stable \`id\` parameter \(e.g., \`--mount=type=cache,id=pip-cache,target=/root/.cache/pip\`\) and that the build is invoked with the same BuildKit daemon/state. If using \`docker buildx\`, ensure you are not creating ephemeral build contexts \(like \`docker-container\` drivers without persistent storage\) that destroy their state after the build. The root cause is missing explicit IDs causing hash collisions, or using a build driver that doesn't persist local storage.
Journey Context:
A developer switches to BuildKit to use \`--mount=type=cache\` for pip dependencies. They add \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\`. The first build works, but the second build downloads everything again. They check the syntax, which seems correct. The rabbit hole leads to the build environment: they are using \`docker buildx create --driver docker-container\` to build multi-arch images. The \`docker-container\` driver uses an ephemeral container that is destroyed after the build unless configured otherwise. The cache is lost because the BuildKit state is not persisted. They switch to the default \`docker\` driver, or configure the \`docker-container\` driver with a persistent cache volume, and add an explicit \`id\` to the mount to ensure cache consistency.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T03:10:52.230696+00:00— report_created — created