Container secrets and exposure prevention
A secret is sensitive data such as a token or password. Container security includes where a secret originates, who may read it, how it reaches a workload, and how it is rotated or revoked. Build-time and runtime secrets require different delivery mechanisms.
Worked example
This is an offline lesson. Scenarios are fictional and Docker commands are unexecuted. Any optional execution requires an approved disposable VM, a reviewed preloaded image with verified digest, blocked external access and appropriate resource limits. No host-policy change, real credentials or live scan is required. Keep the exercise offline. Before any separately authorized runtime check, verify the effective daemon, builder, runner or deployment identity as applicable; use synthetic data, reviewed inputs and bounded resources. Record missing evidence as NOT RUN.
Fictional secret flow (no values)Build dependency -> approved temporary build-secret mountRuntime database -> approved workload-specific secret deliveryApplication -> read only needed value; never log itExposure suspected -> revoke/rotate + assess affected artifacts/logs + rebuild if neededBuild arguments and Dockerfile ENV are unsuitable for sensitive build credentials because values may survive in images, history or build metadata. BuildKit secret mounts limit normal availability to the build instruction, but build code can still copy or log their contents. At runtime, environment values may be visible through inspection or diagnostics. Prefer a scoped secret delivery mechanism appropriate for the deployment. Local Compose file-backed secrets rely on host file access and do not provide the same guarantees as an external managed secret system. A secret omitted from the final image may still exist in a log, build context, cache or source history. If a real secret is exposed, revoke or rotate it through authorized procedures; simply deleting a layer or masking a log is not enough. Secret contents do not normally invalidate the BuildKit cache. After an authorized rotation, ensure required secret-dependent work actually executes by using a reviewed nonsecret cache invalidation input or applicable no-cache policy. Never use the secret itself as a cache key or build argument.
Common mistake
Never use real credentials in training fixtures. A secret mount is not protection against untrusted build code.
Manual practice
For a fictional token accidentally included in a build argument, list the places that might retain it and three incident response actions. Do not generate or use a real token.
Worksheet: lab-fixtures/docker-container-security/09-secret-lifecycle/scenario.txt.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Container secrets and exposure prevention — Knowledge check
Apply defensive container security reasoning in a fictional scenario.
Why avoid build ARG for secrets?
Can untrusted build code copy a mounted secret?
What should follow confirmed credential exposure?