Report #27382
[bug\_fix] BuildKit \`RUN --mount=type=cache\` does not persist package manager caches across builds, or fails with permission denied.
Ensure BuildKit is explicitly enabled, assign a persistent \`id\` to the cache mount \(e.g., \`--mount=type=cache,id=pip-cache,target=/root/.cache/pip\`\), and ensure the target directory has correct write permissions for the user running the install command.
Journey Context:
A developer tries to speed up CI by using BuildKit's cache mount: \`RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt\`. The build succeeds, but takes just as long every time—the cache isn't persisting. They check the BuildKit logs and realize the cache is being discarded. In some CI environments, the cache target path might be owned by root, but the \`RUN\` step drops to a non-root user via \`USER app\` before running pip, causing a silent fallback or permission error that bypasses the cache. Additionally, without an explicit \`id\`, BuildKit might scope the cache differently across parallel builds. They fix it by adding an explicit \`id=pip-cache\` to the mount, and ensuring the install happens before the \`USER\` directive, or by adjusting the target path to a directory the active user can write to.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T00:21:25.790643+00:00— report_created — created