You Rotated the Docker Build Secret. Did the Build Use It?

A green Docker build may never exercise a rotated credential. Trace the cached RUN, force a scoped rerun, and keep secrets separate from freshness inputs.

Share
Orange container layers remain cached beside a key; headline: New secret. Same cached build.

The credential was rotated. The build went green. That sounds reassuring until you ask a slightly awkward question: did the build actually use the new credential?

Docker BuildKit does not include a build secret’s contents in the cache key. If the secret-consuming instruction is cached, changing the token alone won’t rerun it. To check fresh access, deliberately invalidate that stage or change a separate, non-secret revision input. Keep the credential in a secret mount.

This is an AI-prepared, documentation-based explainer, not a personally reproduced incident. The walkthrough and commands below are illustrative and unexecuted. See Debugly’s editorial policy.

The green checkmark answered a smaller question

Imagine a build that downloads a private dependency. Yesterday it authenticated successfully. Today you rotate the token and run the same build. Nothing else changes: same Dockerfile, same dependency lockfile, same build context.

A cache hit can give you yesterday’s result without contacting the private service. The build’s success establishes that an existing result was reusable under the builder’s cache rules. It doesn’t establish that today’s token can authenticate. Your green checkmark has an excellent memory and no recent references.

This distinction matters even when the finished image is completely correct. Credential validation and artifact production are different tasks. A cached artifact can be valid while the replacement token has the wrong scope.

It becomes a correctness problem when the secret changes more than access: perhaps a credential selects a different account’s configuration, or a secret-mounted file also contains dependency selections. Now the input that determines the intended output changed, but the cache didn’t get that information.

First clue: did the fetch instruction execute?

Before replacing another credential, inspect the exact build step that consumes it. Run your normal build with --progress=plain, preserving its normal context, platform, target and cache settings. Find that instruction’s result. A CACHED marker is the clue to investigate; it isn’t evidence of an authentication attempt.

Docker’s cache rules explicitly exclude secret values. Secret IDs and mount paths do affect cache matching, so renaming a mount may appear to fix the problem. That changes a different input. It does not mean BuildKit started noticing secret contents.

There are three useful branches here:

  • The instruction was cached: arrange one intentional rerun before drawing conclusions about the credential.
  • It ran and the private service rejected it: investigate token scope, expiry, endpoint and the identity the service actually saw.
  • It ran but made no network request: investigate the download tool’s own cache or offline behavior. A Docker cache miss doesn’t promise an upstream request.

Also confirm the result you’re inspecting belongs to the build you just ran. An old deployed image and a cached build instruction can produce similar symptoms, but changing build cache won’t repair a deployment pointing at yesterday’s image.

Make the rerun narrow enough to be useful

For a named stage, Buildx provides --no-cache-filter. If your secret-dependent stage is called private_deps, an illustrative diagnostic invocation is:

docker buildx build \
  --progress=plain \
  --no-cache-filter private_deps \
  --secret id=artifact_token,env=ARTIFACT_TOKEN \
  --load -t example/app:credential-check .

This assumes ARTIFACT_TOKEN is already supplied securely to the build client’s environment. Don’t paste a real token into a shell command or print it for debugging. The image name is illustrative; adapt the command to your actual build and output requirements.

The Buildx reference documents stage-specific invalidation. Prefer that scope over clearing a shared builder’s entire cache. If the stage doesn’t exist or isn’t part of the selected build target, correct the build selection first.

Once it runs, inspect what happened at both boundaries: the builder executed the instruction, and the dependency client performed the intended access. Record a non-sensitive artifact version or digest. Where available, inspect the upstream service’s authorized audit record. Don’t log request credentials or response bodies that may contain secrets.

For routine builds, give freshness its own input

If a rotation must trigger a new access check, express that policy explicitly. Use a public revision marker, independently generated from the credential. For example, this illustrative fragment assumes a dependency-fetching program already exists in the build context:

# syntax=docker/dockerfile:1
FROM alpine:3.23 AS private_deps
WORKDIR /work
COPY fetch-private-deps.sh /usr/local/bin/fetch-private-deps

ARG PRIVATE_DEPS_REVISION
RUN --mount=type=secret,id=artifact_token,required=true \
    test -n "$PRIVATE_DEPS_REVISION" && \
    /bin/sh /usr/local/bin/fetch-private-deps \
      /run/secrets/artifact_token /work/deps

The helper must read the mounted credential, authenticate without exposing it, fail on an unsuccessful fetch, and write only intended non-secret dependency artifacts to /work/deps. This is an interface sketch, not a complete downloader or production Dockerfile.

Pass a marker such as --build-arg PRIVATE_DEPS_REVISION=rotation-2026-11-04-a along with the secret. Because that argument is used by the instruction, changing it gives the cache a visible freshness input. Keep the marker stable for ordinary reuse; advance it when your policy requires a new fetch or validation.

Don’t put the token in the argument. Don’t derive the marker by hashing a secret as a casual workaround either: that creates a persistent credential-derived identifier when an independent revision is enough. A revision says “perform this work again”; it needn’t describe the credential.

A secret mount isn’t a cleanup service

Secret mounts make credentials available for a build instruction without requiring you to bake them into ordinary Dockerfile arguments. They don’t prevent your own command from copying the credential, embedding it into generated configuration, or printing it.

That’s a separate failure mode. If your helper writes a credential-bearing config file into its output directory, a fresh build can faithfully produce a fresh leak. Check the output contract as well as the cache contract.

Separate public dependency selections from authentication wherever practical. A lockfile or non-secret manifest can participate in normal build inputs; a credential can remain a secret. If one file contains both, don’t blindly replace a secret mount with COPY just to make cache invalidation work.

What this check still doesn’t prove

A fresh authenticated fetch proves access for that operation at that time. It doesn’t establish that every permission is correct, that a runtime credential was deployed, or that future requests will succeed. Likewise, invalidating the instruction doesn’t refresh every other cache involved or select a different artifact version for you.

Keep immutable artifact identity separate from credential revision. If you want a new dependency version, change the dependency selection. If you want to validate new access, rerun the access operation. If you want to remove leaked credentials, investigate the image and outputs rather than assuming rotation erased historical copies.

For adjacent problems, Debugly’s dependency-layer cache investigation explains accidental cache misses, while container image history forensics covers what an image can retain. Secret rotation without downtime deals with the application handover rather than this build-time boundary. More investigations live in Docker.

Put one explicit line in your rotation runbook: identify the operation that must use the replacement credential, force that operation to execute, and collect non-sensitive evidence that it did. “Build passed” is useful. “New credential exercised” is the fact you were trying to establish.