Report #14228
[bug\_fix] Permission denied when writing to a directory mounted with RUN --mount=type=cache in a Dockerfile using a non-root user.
Specify the uid and gid in the mount instruction: RUN --mount=type=cache,target=/home/user/.cache,uid=1000,gid=1000 ...
Journey Context:
A developer hardens their Dockerfile by switching to a non-root user \(USER app\). They use BuildKit's --mount=type=cache to speed up npm install. The build fails with permission denied when npm tries to write to the cache directory. They add a RUN chown -R app:app /root/.npm step before the mount, but it still fails. The rabbit hole: BuildKit cache mounts are handled at the filesystem level by BuildKit itself, bypassing the normal overlay filesystem layering. They are mounted as root by default, and previous chown commands in the Dockerfile have no effect on the mounted volume. The fix is explicitly declaring the ownership in the mount instruction itself using the uid and gid parameters, ensuring BuildKit creates the cache directory with the correct permissions for the non-root user.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:55:27.442794+00:00— report_created — created