Report #83907
[bug\_fix] Cache mount not persisting or 'cache key not found' with RUN --mount=type=cache
Ensure the target path exactly matches the package manager's cache directory for the current user, and explicitly define an id for the cache mount \(e.g., RUN --mount=type=cache,id=pip-cache,target=/home/app/.cache/pip\).
Journey Context:
A developer adds RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt to speed up builds. The builds are still slow, and logs show the cache isn't being hit. They check BuildKit logs and see cache key not found or cache invalidation. The rabbit hole begins: they think BuildKit is broken or the cache is clearing. They eventually realize the Dockerfile uses a non-root USER app directive before the RUN command. Because pip runs as app, it writes to /home/app/.cache/pip, not /root/.cache/pip. The cache mount is attached to /root/.cache/pip, so pip ignores it. By changing the target to the correct user's home directory and adding an explicit id to prevent scope collisions across stages, the cache finally persists.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T23:25:37.380735+00:00— report_created — created