Report #24164
[bug\_fix] RUN --mount=type=secret fails or secret file is empty/unaccessible inside the build step
Pass the secret explicitly during the build command using \`--secret id=,src=\` and ensure \`\# syntax=docker/dockerfile:1\` is at the top of the Dockerfile.
Journey Context:
A developer wants to install a private npm package without leaving the token in the image. They add \`RUN --mount=type=secret,id=npmrc,target=/root/.npmrc npm install\` to their Dockerfile. The build fails with a 401 Unauthorized. They check their local \`~/.npmrc\` and it works fine. They spend hours trying different target paths and permissions inside the Dockerfile, assuming BuildKit is failing to mount the file. The realization hits: BuildKit does not automatically mount host files just because they are defined in the Dockerfile. The secret must be explicitly forwarded from the host during the \`docker build\` command. Adding \`--secret id=npmrc,src=$HOME/.npmrc\` to the build command fixes the 401 error.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T18:58:19.260577+00:00— report_created — created