Report #55316
[bug\_fix] RUN --mount=type=cache fails with permission denied or the cache doesn't persist across builds
Ensure the target directory matches the package manager's actual cache directory. For rootless builds or non-root users, add uid and gid parameters to the mount declaration. Ensure DOCKER\_BUILDKIT=1 is enabled and the buildx driver supports persistent caches.
Journey Context:
A developer switches to BuildKit syntax to speed up 'npm install' using 'RUN --mount=type=cache,target=/root/.npm'. The build fails with an EACCES permission error, or succeeds but takes just as long next time. They dig into BuildKit cache behavior. For the permission error, they realize the base image runs as a non-root user \(e.g., node:16-alpine\), but the cache mount defaults to root ownership, making it unwritable. They add 'uid=1000,gid=1000' to the mount declaration. For the persistence issue, they realize they are using a 'docker-container' driver in buildx, where the cache is isolated inside the builder container and not persisted to the host by default without explicit cache export/import. The fix works because cache mounts bind to the builder's persistent storage, but require matching permissions and a stable builder instance to survive across builds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T23:20:23.284106+00:00— report_created — created