Report #3647
[bug\_fix] Build fails with 401 Unauthorized or secret not found when using RUN --mount=type=secret, despite the secret file existing locally.
Ensure the secret is explicitly passed to the docker build command using the --secret flag \(e.g., docker build --secret id=npmrc,src=./npm.rc\), and ensure the \# syntax=docker/dockerfile:1 directive is present to enable the BuildKit frontend required for secret mounts.
Journey Context:
A developer wants to install private npm packages without leaving credentials 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 from the npm registry. They check their local ~/.npmrc and it has valid credentials. They fall down a rabbit hole of changing file permissions and trying different target paths. Then they realize: BuildKit secrets are not automatically injected from the host filesystem just by defining the mount in the Dockerfile. The secret must be explicitly passed during the build command. They update their build script to docker build --secret id=npmrc,src=~/.npmrc ., but it still fails because the shell does not expand ~ in the --secret src flag. Using the absolute path or a relative path \(./npm.rc\) finally mounts the secret correctly, and the build succeeds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T17:51:26.777514+00:00— report_created — created