A build secret is mounted with RUN --mount=type=secret for a single step only and never ends up in the image. With required=true the build stops immediately if the call does not pass the secret – instead of silently running on with an empty token.
Symptom
docker build --network=host --no-cache-filter artifacts \ -t notes-api:tag13 notes-api 2>&1 | grep -i -m1 error ERROR: failed to solve: secret artifact_token: not found
In the artifacts stage the build fetches a tool from an artifact server that requires a token. The call above passes no --secret.
Diagnosis
The ID in the error must match the ID in the Dockerfile and in the call. --secret has three forms:
--secret id=artifact_token,src=secrets/artifact_token.txt --secret id=artifact_token,env=ARTIFACT_TOKEN --secret id=artifact_token
Causes
no --secretThe call does not pass the secret
With required=true in the mount, BuildKit stops if no secret with that ID is passed.
short formThe short form looks in the wrong place
--secret id=artifact_token first looks for an environment variable artifact_token, then for a file of the same name in the current folder. If the token lives elsewhere, BuildKit does not find it.
Fix
export ARTIFACT_TOKEN="$(cat secrets/artifact_token.txt)" docker build --network=host --secret id=artifact_token,env=ARTIFACT_TOKEN \ -t notes-api:tag13 notes-api => [artifacts 2/2] RUN --mount=type=secret,id=artifact_token,env=ARTIFACT_T 0.4s
Don't: Pass the token as ARG or ENV – it stays in the history and the image config, readable by anyone who can pull the image.
Dockerfiles hands-on, Day 13: build secrets with RUN --mount=type=secret and SSH mounts, proof that no token is in the image.
Go to day 13 →
FAQ
Why required=true?
Without required=true the step would run on without the secret and the tool would get an empty header – the error would surface much later.
How do I check that the token is not in the image?
Unpack the image with docker save and search the layers for the token; the history shows no trace (Day 11 and 13).