Build secrets and SSH mounts: temporary access
A small example
FROM busybox:1.37RUN --mount=type=secret,id=demo,required=true test -s /run/secrets/demoRUN printf "checked\n" > /result.txtCMD ["cat", "/result.txt"]The CLI passes demo.txt under the ID demo, and the build step reads a temporary file at /run/secrets/demo. The step only checks that the fixture is nonempty; it neither prints nor copies it. The ignore file excludes the fixture from the ordinary build context; the explicit secret input is separate. Secret contents do not automatically invalidate a cached step. If freshness matters, bypass relevant result caching or change a separate nonsecret revision input. SSH mounts provide temporary access to an SSH agent or key for approved tasks; a build command with that access can request authentication. This lesson does not forward an agent, use a real key or contact a repository. Use --no-cache for the positive synthetic-secret build so the required mount is exercised rather than satisfied by a prior result. A required mount is enforced during execution, not evidence that a cache hit accessed the secret.
Try it in a local lab
Manual lab, not a ByteMind executable exercise. Use a new approved disposable Linux VM with blocked network egress and a local Engine and Buildx docker driver. Check docker context show, docker version, docker buildx version and docker buildx ls before doing anything. Confirm the reviewed, preloaded busybox:1.37 for the native platform, expected default user, no entrypoint, required applets and bundled frontend support for RUN mounts. Record its actual digest and versions. If a dependency needs to be fetched or the builder is remote, stop. --network none restricts RUN networking, not all builder traffic. Confirm all bm-v09 tags are unused. Run commands individually in the named fixture directory, stopping on any unexpected failure. All values are synthetic; do not use real credentials. Expected results are predictions, not observed output. Before querying or building, confirm DOCKER_HOST, DOCKER_CONTEXT and BUILDX_BUILDER overrides and the effective endpoint are approved; do not contact an unapproved endpoint. docker build normally uses the Engine default builder, not necessarily the starred Buildx builder. Record the effective local docker driver and platform; review the synthetic context and ignore rules. After each build, require exit status zero before running its tag; an earlier tag may still exist after failure.
Open lab-fixtures/docker-builds-and-caching/07-build-secrets/ in the extracted archive. The Dockerfile and these files are supplied. Work on a disposable copy, not the original archive.
docker build --no-cache --load --network none --progress=plain --secret id=demo,src=demo.txt -t bm-v09-build-secrets:lab .docker run --rm --pull=never --network none bm-v09-build-secrets:labdocker run --rm --pull=never --network none bm-v09-build-secrets:lab sh -c "test ! -e /run/secrets/demo"Expect checked and a successful absence test. The test checks only that specific path; it is not proof no secret was leaked. With --no-cache and without the required secret, the build should fail; reason about that variation offline rather than running it against real credentials.
Confirm the tag belongs to this lab and no containers depend on it. Remove only that tag without forcing. Cached data may remain; dispose of the VM rather than clearing a shared cache. Restore any edited fixture or discard the working copy.
Common mistake
Never print or copy a secret, forward a personal SSH agent to an untrusted build, or include credentials in the context. A secret mount is not a sandbox or leak detector.
Manual practice
Explain how a build might accidentally retain a mounted secret and propose a code-review check. Compare the scope of a file secret and agent access without using any real credentials.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Build secrets and SSH mounts: temporary access — Knowledge check
Apply the build concept to a new scenario.
When the RUN instruction actually executes, what does required=true do?
Does changing only a secret automatically invalidate cache?
What is the safest SSH demonstration here?