Report #94765
[bug\_fix] ERROR: failed to fetch secret : secret not found or access to the requested resource is unauthorized during RUN --mount=type=secret
Pass the secret to the build command using \`--secret id=,src=\` \(e.g., \`docker build --secret id=ssh,src=$HOME/.ssh/id\_rsa .\`\) and ensure the \`RUN\` instruction uses \`--mount=type=secret,id=\`.
Journey Context:
A developer tries to clone a private git repo or pull from a private package registry inside a Docker build. They use \`RUN --mount=type=secret,id=git\_token git clone ...\`. The build fails with 'secret not found'. They check their local environment—the token exists as an environment variable. They try \`ARG\` to pass it, but realize that exposes it in the image history. The rabbit hole is realizing that BuildKit secrets are not automatically pulled from environment variables or files. They must be explicitly mounted \*and\* explicitly passed via the \`docker build\` CLI command. The fix is adding \`--secret id=git\_token,env=GIT\_TOKEN\` \(or \`src=...\`\) to the \`docker build\` command, ensuring the ID matches the \`RUN --mount\` ID exactly. This securely injects the secret into the build step without persisting it in the image layer.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T17:38:44.583427+00:00— report_created — created