Report #72082
[bug\_fix] permission denied writing to directory mounted with --mount=type=cache
Add uid and gid options to the cache mount matching the non-root user \(e.g., --mount=type=cache,target=/app/cache,uid=1000,gid=1000\), or ensure the directory is created and chowned before the USER directive.
Journey Context:
A developer secures their container by switching to a non-root user via the USER app directive. To speed up builds, they add a BuildKit cache mount for their package manager's cache directory. The build suddenly fails with a 'permission denied' error when the package manager tries to write to the cache directory. They try adding chmod or chown commands in previous RUN steps, but the error persists. After digging into BuildKit documentation, they realize that cache mounts are initialized by the BuildKit daemon, which defaults to root ownership \(uid 0\). When the mount overlays the target directory, it ignores the underlying filesystem permissions. The fix is to explicitly tell BuildKit to create the cache directory with the correct ownership by appending uid=1000,gid=1000 to the mount options, aligning the cache mount's ownership with the non-root user.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T03:34:28.834290+00:00— report_created — created