Prompt
What is the best way to inject secrets into containers?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
The best practice is:
-
Do not bake secrets into the image
- Never hardcode secrets in Dockerfiles, source code, or environment files committed to Git.
- Images should be reusable and non-sensitive.
-
Use a secrets manager
- Prefer a dedicated secret store such as:
- HashiCorp Vault
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- Google Secret Manager
- Kubernetes Secrets only if you understand their limitations and secure them properly
- Prefer a dedicated secret store such as:
-
Inject secrets at runtime, not build time Good patterns include:
- Mounted files/volumes: secret is written to a file inside the container
- Environment variables: convenient, but less ideal because they can be exposed via process inspection, crash dumps, or logs
- Sidecar/agent-based retrieval: app fetches or receives secrets from a local agent
-
Prefer file-based secrets over environment variables
- Many runtimes can read secrets from files more safely.
- This reduces accidental exposure in logs and diagnostic tools.
-
Use orchestration-native secret mechanisms
- Kubernetes: mount Secrets as files; use external secret operators if possible
- Docker Swarm: use Docker secrets
- ECS/EKS/GKE/AKS: integrate with cloud secret services
-
Rotate and audit secrets
- Automate rotation where possible.
- Restrict access with least privilege.
- Log access to secrets and monitor for misuse.
-
Avoid passing secrets in command-line arguments
- They can show up in process lists and shell history.
Practical recommendation
If you’re choosing one general approach:
Store secrets in a secret manager and mount them into containers as files at runtime, using short-lived credentials if possible.
If you want, I can also give you:
- a Kubernetes-specific recommended pattern
- a Docker Compose example
- or a comparison of env vars vs mounted files vs secret managers