Report #3076
[bug\_fix] Private package manager authentication fails during docker build \(401 Unauthorized or Permission denied\) even when using RUN --mount=type=secret.
Ensure the secret is correctly mounted into the container using the target path inside the RUN instruction \(e.g., --mount=type=secret,id=mysecret,target=/root/.npmrc\) and pass the secret during the build command using docker build --secret id=mysecret,src=./npmrc.
Journey Context:
A developer wants to install a private npm package. They try passing a Personal Access Token via ARG, but realize this bakes the secret into the image history. They switch to BuildKit's --mount=type=secret. The build fails with a 401 Unauthorized. They debug by printing environment variables and files inside the RUN step, realizing the secret is empty. They forgot to pass the --secret flag in the docker build command, or they mismatched the id. Once they align the id in the Dockerfile with the id in the docker build command, and explicitly set the target path where the package manager expects it, the secret is mounted as a tmpfs during that specific RUN step and unmounted afterward. This works because it securely injects credentials only for the exact step that needs them, keeping the final image and layer cache completely free of secrets.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T15:20:02.555273+00:00— report_created — created