Report #104567
[bug\_fix] BuildKit cache mount not persisting across builds: /var/cache/apt empty each time
Use a persistent cache mount with a unique ID: \`--mount=type=cache,target=/var/cache/apt,id=apt-cache\`. Ensure the cache is not cleared by \`apt-get clean\` in the same RUN. Also, use \`--cache-from\` for registry cache if builds run on different machines.
Journey Context:
A team used BuildKit's cache mounts to speed up apt package installations: \`RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y python3\`. However, each build still downloaded packages from scratch. They discovered that without an explicit \`id\`, BuildKit uses a default cache ID based on the mount target path, but if the build runs on different hosts \(e.g., CI runners\), the cache is local and not shared. Also, the \`apt-get clean\` command in the same RUN would delete the cache. The fix was to add an \`id\` \(e.g., \`apt-cache\`\) to make the cache mount stable across builds on the same machine, and to avoid cleaning the cache in that RUN. For multi-machine CI, they needed to push the cache to a registry using \`--cache-to\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-09-06T20:06:22.315222+00:00— report_created — created