Docker Build Secrets: Why Temporary Mounts Do Not Prove a Leak-Free Build
CONTAINER BUILD SECURITY
A temporary secret mount changes how a build receives a credential. It does not make the code receiving that credential trustworthy, or make a cached result evidence of fresh authentication.
By Hossam
The mount disappeared. Did the secret?
A container build needs a private dependency. The team replaces a Dockerfile build argument with a BuildKit secret mount, then checks that the mounted credential is absent from the finished container. That is an improvement, but it does not answer every security question about the build.
The receiving command could have copied the value into another file, printed it into a retained log, or passed it to code that should not receive it. A successful rebuild could also have reused a cached result without using the newly rotated credential at all. Credential delivery, permitted use, persistent output and fresh execution are separate checks.
This guide is documentation-based analysis of Docker and BuildKit behavior. No build was executed for this article. The review scenarios below are fictional, not incident reports or measured tests. Linux-style paths are used for discussion; Windows container handling has separate documented details.
Sources: Build secrets, Dockerfile reference, BuildKit security boundary.
ARG and ENV fail in different ways
Docker advises against putting secrets in Dockerfile ARG or ENV. ENV values persist in the image configuration and in containers created from that image. ARG is not automatically a runtime environment variable, but Docker warns that its values may persist in image metadata, provenance attestations and image history.
That distinction matters when investigating a leak. A missing value in the final container environment does not prove that a build argument was confidential. Inspecting only the running container can miss the metadata and history that were distributed with the image.
Use build arguments for non-secret build parameters, such as a dependency version or a deliberately non-sensitive rebuild marker. Moving a credential from ENV to ARG is not the security migration. Moving its delivery to the supported secret mechanism is the starting point, followed by a review of what consumes it.
Sources: Build variables, SecretsUsedInArgOrEnv.
The client source and the RUN destination are different
Docker describes secret use as two steps: supply a secret to the build client, then consume it in the intended Dockerfile instruction. The client can read the value from a file or an environment variable. The RUN secret mount can expose it as a file or as an environment variable for that instruction.
For example, the env option on a client-side secret declaration identifies the source variable on the client. The env option on a RUN secret mount identifies the variable presented to the receiving build command. Treating those two settings as interchangeable makes it harder to review which process gets the value.
The default file destination follows /run/secrets/ plus the secret ID when the documented default applies. Custom target paths are supported. Match the ID between delivery and consumption, and review only the instructions that actually need the credential rather than extending its scope for convenience.
Sources: Build secrets, Dockerfile reference.
Temporary delivery does not prevent persistent copies
A secret mount avoids baking the mounted value into the image or build cache through that delivery mechanism. It does not prevent the receiving application from writing the value into ordinary output. BuildKit explicitly notes that an untrusted frontend can run a container with a supplied secret mounted and read its value.
Consider two hypothetical failures: a command prints a mounted value into a retained build log, or copies it into a normal file that becomes part of the image output. These are conceptual failure cases, not experiments performed for this article. Their practical lesson follows the documented trust boundary: the program authorized to read a credential can misuse it.
Review shell tracing, debug output, generated configuration and package-tool behavior around the consuming step. Do not print a real value merely to prove that the mount works. A log scrubber or a scan of the final filesystem is a separate safeguard, not a guarantee supplied by the mount.
Sources: Dockerfile reference, BuildKit security boundary.
Keep context admission separate from secret delivery
A build context makes files available to the build. Passing a credential correctly as a secret does not undo a second copy accidentally included in that context and later copied by a broad COPY instruction.
Docker supports .dockerignore to remove matching files from the context before it is sent to the builder. When a Dockerfile-specific ignore file exists, Docker documents that it takes precedence over the root .dockerignore. Review the actual context path and applicable ignore rules for each CI job, rather than assuming that one repository-root rule protects every invocation.
Keeping a local credential source outside the chosen context can reduce accidental inclusion. That is a useful layout choice, not permission to skip the context review. Generated files, alternate context directories and exception patterns can change what the builder receives.
Sources: Build secrets, Build context.
Rotation does not invalidate the build cache
Docker states that secret contents are not part of the build cache. Changing a secret value therefore does not invalidate the cached instruction. Secret properties such as the ID and mount path do participate in the cache checksum, which is a different rule from hashing the credential itself.
That can surprise a team that rotates a token, starts the same build and interprets a green result as proof of access with the replacement token. If the relevant instruction is reused from cache, that result does not establish fresh authentication to the private service.
When fresh execution is required, make it an explicit build requirement. Docker documents cache-control options, including a stage-specific no-cache filter, and a non-secret build argument changed alongside secret rotation as a cache-invalidation technique. Do not put the secret value into that argument. The aim is to request a new execution without reintroducing the original exposure path.
Sources: Cache invalidation.
required=true is not a complete CI authentication test
The Dockerfile reference makes secret presence optional by default. Setting required=true makes the instruction error when its needed secret is unavailable. This is useful for steps that must not silently fall back to an unauthenticated mode.
The scope of that setting still matters. It declares that a needed secret must be available to the instruction. It does not establish that a remote service accepted the credential, that the account has the desired access, or that the downloaded dependency is the intended one. Those conclusions need evidence from the consuming operation.
A CI acceptance check that must establish present-day access should force the relevant step to run, require the secret, and check the actual private dependency or service response. A cached successful artifact may be exactly what a normal reproducible build needs, but it is not the same test as validating a newly issued credential.
Sources: Cache invalidation, Dockerfile reference.
A build check is not a secret-leak audit
Docker's SecretsUsedInArgOrEnv rule reports ARG and ENV keys that indicate sensitive data. That catches a useful class of mistakes before a full build, including plainly named password or token variables.
The rule is described in terms of variable names. It is not a promise to track every byte through a package manager, inspect every generated file, or prove that logs contain no confidential data. A clean result should not be reported as evidence that no credential can leak.
Use the check as one layer of review. Check the source of credentials, the receiving command, output files, image metadata, retained logs and any distributed build artifacts separately. State what each check covered so that a passing label does not acquire a broader meaning than its evidence.
Sources: SecretsUsedInArgOrEnv, BuildKit security boundary.
Choose the authentication boundary before choosing syntax
Docker distinguishes general-purpose secret mounts, SSH mounts and Git authentication for remote contexts. A private repository used as the build context may need credentials before any Dockerfile RUN instruction can execute. Supplying a secret to a later package-download step does not solve that earlier context-fetch problem.
SSH mounts serve SSH agent sockets or keys, while remote Git authentication has its own documented credentials and host scoping. Do not interchange these mechanisms merely because they all involve private access. Match the credential to the operation that needs it and the code that will receive it.
This also keeps the trust review specific. A build definition allowed to consume a package token should not automatically receive unrelated credentials. Review short-lived, narrowly scoped credentials and the builder environment as independent controls, not as properties automatically created by a secret mount.
Sources: Build secrets, BuildKit security boundary.
A fictional review with two passing builds
Imagine Cedar changes a private package token and starts its usual build. The first run is green, but the package-install instruction is marked cached. That run may produce the expected artifact, yet it has not demonstrated that the new token works. Cedar records the cache reuse instead of claiming a successful credential-rotation test.
For the second run, Cedar deliberately executes the credential-consuming step. Authentication succeeds, but a verbose helper writes its generated authorization configuration into an ordinary output directory. The mount itself disappears as intended. The output review still fails because the helper created a separate persistent copy.
These invented outcomes point to different fixes: execution evidence for the first, consumer and output changes for the second. Replacing every build with an uncached build would not, by itself, repair the helper that writes confidential data. Changing the helper would not make a cached result proof of fresh authentication.
Sources: Cache invalidation, Dockerfile reference, BuildKit security boundary.
Related reading
Credential handling is separate from artifact identity. See how image digests, signatures and provenance contribute different deployment evidence.
Sources and scope
This guide uses Docker documentation and the BuildKit project's stated security boundary. Documentation can change; check the Dockerfile frontend, builder version and platform used in your environment. It does not claim a hands-on audit, a universal leak detector or a tested production configuration.
Comments
Post a Comment