Database code, API token, private key, and registry credential are not just variable settings; disclosure of them can create direct access. Placing secret in a Dockerfile, repository, or image and deleting it at the next layer does not solve the problem.
Quick answer:Do not keep the secret in the source, Dockerfile, image, and log. Use the secret manager or limited file for build and runtime. Each service receives only the required secret; reads the file program, reports the error without printing the value, and supports rotation without depending on the new image.
First, make an inventory.
Before choosing a tool, list what secrets are, who owns the business and technical information, what services they use, and what scope they are compromised if disclosed. Shared credentials between multiple environments or applications make it difficult to detect and disable. Keep separate accounts for development, staging, and production so that access to the laptop is not transferred to the live system.
Secret Build time is different from Secret Runtime time.
The token receipt of a private package may only be required in the build; the database password is used while the container is running. The transfer of both is dangerous. The build secret should only be available in the same build command and not inside the artifact. Runtime secret should be provided when the service is running and does not require a rebuild image to be changed. Enter both paths separately in the threat model and CI.
- What?ARGAnd theENVThey're not good for Build Secret?
The arguments and build variables can remain in metadata, history, cache, or log. Deleting a file or unset in a subsequent command does not delete the previous layer. Docker documents use secret mount or SSH mount to build time credentials.Checklist of Dockerfile productionExplains the build and runtime boundary in more detail.
How is BuildKit Secret used?
# syntax=docker/dockerfile:1
RUN --mount=type=secret,id=npm_token,required=true NPM_TOKEN="$(cat /run/secrets/npm_token)" npm ci
This is just an example: your package tool may want a special config file and should not print the token in the verbose output. secret is then mounted and the goal is to not be left in the final layer. After checking build, image and log for credential rejection. If the command copies the credential file in cache or home, using mount alone does not prevent disclosure.
When should we use SSH mount?
SSH forwarding is used to clone a private source so that the key inside the image is not copied. Do not delete host key verification; connecting to an anonymous host can expose the credential or source to attack. Deploy key with read-only and limited access to the same repository is better than a wide personal key.
The Secret in Docker Compose
Compose secret is defined at the top level and then assigned to the user service only./run/secrets/<name>This model of environment variable is less exposed and provides service access control; however, the source file on the host must still be protected with appropriate permission, backup, and access. Do not assume that normal compose is the same as secret encrypted in Swarm or secret manager cloud.
services:
app:
image: registry.example/app@sha256:...
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password
This template does not include values in YAML, but does not secure the path on the host. The secret folder should not be entered into the repository or build context. Image names and digest are samples and should be replaced with the actual artifact.
The contract._FILEIt's not public.
Some official images are variable, likePOSTGRES_PASSWORD_FILEIt's an image implementation contract, not the capability that any automated program supports. Read accurate image documentation. You can configure the secret file path for your program; keep the value only in the memory and never print it on an error page or endpoint.
What are the risks of Environment Variable?
The environment variable may be seen in the inspect, crash report, debug dump or log tools and is accessible to all related processes. Sometimes the platform offers only this method; in that case, make scope credential, access to Docker daemon, log redaction, and rotation more difficult.
Private Key and TLS certificate
Manage the private key like a high-impact password. Only the reverse proxy needs to read it; the apps behind the proxy should not receive the public key of the site. mount only requires reading, user-restricted and extension/reload cycles.The TLS guide to Docker servicesIt shows why a new file without reloading and external testing is not enough.
The least privilege on three levels.
- A credential only has the necessary operating permissions, such as read-only to a specific bucket.
- Secret is only given to the consumer's container and process.
- Humans and pipelines have access only in the right time and environment.
A token administrator ruins rotation and attribution for all services. The independent service account reduces scope, limited scope, and expiration date of the event. Access to the Docker socket can virtually be a way to read the secrets of other containers; do not mount it to agents and unnecessary tools.
Design the rotation before the crisis.
Create a new Secret, change the user to take a new value, validate the health, and then revoke the previous value. For credentials that have the dual-key convergence potential, this reduces downtime. If the application only reads the file in startup, changing the file alone is not enough and controlled recreate/reload is required. Choose a stable copy name or reference that matches the secret manager.
What if Secret gets exposed?
- Identify the scope of impact and consumer systems.
- Revocate or rotate the credential; it's not enough to delete it from Git.
- Check log access and unusual use in the disclosure area.
- Identify the image, cache CI, artifact and backup versions that have value.
- Fix the cause of the secret entry to the wrong path and add automatic guard.
Rewrite the repository history may reduce the next release, but it will not recover the cloned versions. First, delete the credential. Document the incident details without re-publishing your secret.
Detection and Audit
Secret scanning is useful in commit and CI but has false positive and blind spot. Combine known patterns, entropy and lists of providers with human review. Monitor secret manager access, policy changes, frequent failures of authentication, and use of the new principle.
Common Mistakes
- Putting the secret in.
.envAnd assuming it was safe because Git ignored it. - Copy the secret to the Dockerfile and delete it to the next layer.
- Print the full config in the log for error
- Sharing a credential across all environments
- Mounting secret for services that don't use it.
- File rotation without program reload
- Long-term key maintenance in the CI general runner
Checklist of actions
Record the scope, expiration date, injection method, rotation behavior, and owner for each secret. Review the repository, Dockerfile, final compose, image history, and log pipeline. Perform an experimental rotation in staging and also test the failure mode, which must fail closed and give a message without a sensitive value.
When do you need special assistance?
If the secrets are spread between CI, registry, multiple services, and multiple servers, a one-time shift may create a complete or lockout.The application is Dockerized.It can redesign build and runtime paths, access levels, and rotation in stages.
Common Questions
Is the file.envIs he a secret manager?
No, it's just a config input, and its protection depends on the file system, process, and host access.
Is the Compose Secret encrypted?
Don't assume that the normal Compose behavior is the same as Swarm; check the host source file and platform model.
Can you put the secret in the image because the registry is private?
No; an image can be cached, exported or moved to another environment, and its rotation depends on rebuilding all versions.
Should the container restart after the secret change?
Depending on the application, whether it's just reading when you start, recreate or reload. Document and test this behavior.