Report #81721
[bug\_fix] E: Could not open file - WriteError or permission denied when writing to a cache directory using \`RUN --mount=type=cache\`
Specify the UID/GID of the cache directory in the mount declaration to match the non-root user running the command: \`--mount=type=cache,target=/home/app/.npm,uid=1000,gid=1000\`.
Journey Context:
A developer uses \`RUN --mount=type=cache,target=/home/app/.npm npm install\` to speed up dependency installation. The container runs as a non-root user \(\`USER app\`\). The build fails with permission denied errors when npm tries to write to the cache. The rabbit hole involves trying to \`chown\` the directory before the mount or running \`npm cache verify\`. The root cause is that BuildKit creates the cache mount directory as root by default. Because the mount happens \*before\* the \`RUN\` step executes, any \`chown\` in a previous layer is overwritten by the root-owned mount. The fix is to explicitly tell BuildKit to create the cache directory with the correct ownership using the \`uid\` and \`gid\` mount options, ensuring the non-root user can read and write to the persistent cache.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T19:46:04.543626+00:00— report_created — created