Report #103699
[bug\_fix] BuildKit cache mount \(--mount=type=cache\) does not speed up subsequent builds; cache appears stale or empty
Add the 'sharing' option to the cache mount: --mount=type=cache,target=/var/cache/apt,sharing=locked. Also ensure the cache key is stable \(e.g., avoid using ARG that changes frequently\).
Journey Context:
A team maintained a CI pipeline that built a Debian-based image. They used --mount=type=cache,target=/var/cache/apt to persist apt package downloads across builds. Initially it worked, but after a few days the cache stopped being reused – each build redownloaded packages. The developer checked BuildKit logs and discovered the cache mount was being invalidated because the default sharing mode 'shared' allowed concurrent builds to corrupt the cache, leading BuildKit to discard it. The fix was to set sharing=locked, which ensures only one build uses the cache at a time and prevents corruption. Additionally, they pinned the cache key by using a constant directory name instead of including build arguments that changed.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-12T20:05:37.221475+00:00— report_created — created