Container layers can expose secrets in public registries because each pushed layer may still contain deleted files, shell history, or build arguments from earlier steps. Anyone with pull access can inspect those layers and recover credentials unless the image is rebuilt cleanly and the exposed secret is rotated.
How container layers turn deleted secrets into registry exposure
Container images are not a single flat artifact. They are built as stacked layers, and each layer can preserve the file contents, metadata, and build-time inputs that existed when that layer was created. If a secret was added earlier and then deleted later, the deleted content may still be recoverable from the image history or from an older layer stored in the registry.
That is why “removing” a secret in a later Dockerfile step does not necessarily remove it from the published image. The safer mental model is that every layer may be inspectable by anyone who can pull the image, especially in a public registry where no additional access barrier exists.
Build arguments, environment variables, shell history, copied dotfiles, and temporary files are common ways secrets leak into layers. Once the image is pushed, those artifacts can travel with it unless the image is rebuilt from a clean state and the secret is replaced at the source.
Why public registries make layer residue harder to ignore
Public registries expand the audience for any mistake. If a layer contains a token, password, private key, or connection string, a broad set of users can download the image and inspect its layers offline. That turns a build mistake into a disclosure event, even when the running container never prints the secret.
The practical risk is not limited to the final filesystem view inside the container. Registry consumers can often recover secrets from image metadata, layer diffs, or the build context that was copied into the image. For that reason, image scanning needs to examine layers and history, not just the final container filesystem.
Where layer leakage is possible, registry controls matter as much as image hardening. Use digest-pinned pulls, enforce registry access controls where available, and treat public publication as irreversible exposure once a secret has been baked into any layer.
What a secure response looks like when a secret has already been pushed
The first priority is to assume the secret is compromised, not merely “present in an image.” If the secret can authenticate to a service, rotate it promptly and invalidate the old value so that the leaked layer cannot continue to work after discovery.
Then rebuild the image cleanly so the secret never enters any new layer. That usually means removing the secret from the Dockerfile, replacing build-arg patterns with secret mounts or equivalent ephemeral injection, and checking that no generated files, package caches, or history entries still contain the value.
Finally, verify the registry itself. Pull the image, inspect its history, and scan each layer for credentials, tokens, keys, and other secret material. A successful rebuild is only complete when the artifact no longer contains recoverable secret residue anywhere in its layer stack.
Risk and Threat Considerations
Layer residue creates a disclosure path that survives ordinary deletion logic. A secret can be removed from the working filesystem and still remain in an earlier layer, so the exposure persists until the image is rebuilt and any affected secret is rotated.
Failure mechanism: An attacker or unauthorized registry user pulls the image, inspects the layer history or diff data, and recovers credentials that were assumed to be gone.
Impact: Recovered secrets can enable registry access, source code access, cloud API calls, or downstream service compromise, depending on what the leaked credential authorizes.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Non-Human Identity Top 10 addresses the attack and risk surface, while NIST SP 800-190 and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-02 — Secret Leakage | Image layers can expose secrets in public registries. |
| NHI-07 — Long-Lived Secrets | Leaked layer secrets remain useful if they are not short-lived or revocable. | |
| Recommendation — Scan registry images for leaked secrets and rotate any exposed credentials immediately. Replace persistent secrets with short-lived credentials and enforce rotation on exposure. | ||
| NIST SP 800-190 | N/A — Application Container Security Guide | Container images and registries are the exact security boundary in this question. |
| Recommendation — Apply container-image controls for build hygiene, registry protection, and artifact inspection. | ||
| NIST SP 800-53 Rev 5 | IA-5 — Authenticator Management | Exposed credentials from image layers require lifecycle control and revocation. |
| CM-6 — Configuration Settings | Clean rebuilds depend on preventing secrets from entering image configuration and layers. | |
| Recommendation — Revoke and replace any authenticator that may have been embedded in an image layer. Harden build configuration so secrets are never written into image layers or metadata. | ||
Practitioner Guidance
What to verify: Confirm whether the secret exists in image history, layer contents, or build artifacts, not just in the final running container. If any one of those still contains the value, treat the image as exposed.
Decision rule: If the leaked item can authenticate to anything material, rotate it before or alongside the rebuild. If it only appears to be a test value, validate that assumption against the service it might reach, not against the Dockerfile alone.
Common mistake: Deleting a file in a later layer and assuming the registry copy is clean. That pattern often leaves the sensitive data recoverable in an earlier layer, which is exactly what scanners and attackers look for.
Practitioner takeaway: The right response is not “hide the secret better in the image,” but “ensure the secret never enters a published layer, and assume rotation is required if it already did.”
Related resources from NHI Mgmt Group
- How should security teams prevent secrets from leaking into container image layers and public registries?
- Who is accountable when a public application flaw leads to secret exposure and code execution?
- How should security teams reduce exposure when a private container registry might leak image layers or history?
- How should security teams reduce secret exposure in public API workspaces and shared collections?
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on December 3, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org