Report #82796
[bug\_fix] ERROR: failed to solve: failed to compute cache key: mount callback failed on cache / mkdir: cannot create directory '...': Permission denied
Create the target directory in a previous RUN step before mounting the cache, and ensure the UID/GID of the mount matches the non-root user using --mount=type=cache,target=...,uid=1000,gid=1000.
Journey Context:
A developer switches their Dockerfile to run as a non-root user via the USER directive and uses RUN --mount=type=cache,target=/root/.npm to cache npm packages. The build fails with a permission denied error when trying to write to the cache directory. They attempt to chown the directory in a prior RUN step, but the error persists. Digging into BuildKit behavior, they learn that cache mounts are handled at the filesystem level by BuildKit and bypass the normal layer filesystem. If the cache target doesn't exist, BuildKit creates it as root. Since the container is running as a non-root user, it cannot write to the root-owned cache directory. Explicitly creating the directory beforehand and specifying the uid/gid on the mount flag aligns the cache ownership with the active user.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T21:33:38.610789+00:00— report_created — created