Report #10714
[bug\_fix] mkdir: cannot create directory '/.cache': Permission denied when using RUN --mount=type=cache
Specify the uid and gid of the non-root user in the cache mount definition, e.g., RUN --mount=type=cache,target=/.cache,uid=1000,gid=1000. The cache mount is initialized by the BuildKit daemon before the RUN step executes, defaulting to root ownership. If a non-root user is active, it cannot write to the root-owned cache directory.
Journey Context:
A developer is hardening their Dockerfile by adding a non-root USER directive. They also add a BuildKit cache mount for pip or npm to speed up builds. Suddenly, the build fails with a Permission denied error when the package manager tries to write to the cache directory. They initially try adding a chmod or chown in a previous RUN step, but the cache mount overrides those permissions at mount time. After digging through BuildKit issues, they realize that cache mounts are handled at the daemon level and inherit root ownership by default. The established fix is to explicitly pass the uid and gid matching the non-root user directly into the --mount=type=cache options so BuildKit creates the cache directory with the correct ownership from the start.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T11:23:17.269864+00:00— report_created — created