Prompt
What should I use for secret injection into containers?
Latest observation
For secret injection into containers, the best choice depends on your platform and how much operational complexity you want to take on. A good rule is: prefer runtime injection from a secret manager over baking secrets into images or environment files.
Best-practice options
1. Kubernetes: External Secrets + a secret manager
If you’re on Kubernetes, a common and solid pattern is:
- Secrets Manager: AWS Secrets Manager, Azure Key Vault, Google Secret Manager, HashiCorp Vault
- Injection layer:
- External Secrets Operator if you want Kubernetes Secrets synced from an external store
- Secrets Store CSI Driver if you want secrets mounted directly as files
Recommended when: you want central secret management, rotation, and least privilege.
Pros
- No secrets in images or Git
- Works well with rotation
- Integrates with cloud IAM
Cons
- More setup than plain env vars
- Requires external secret backend
2. Kubernetes: Secrets Store CSI Driver
This is often the best option if you want secrets mounted as files inside pods without persisting them as Kubernetes Secrets.
Recommended when: you want secrets to exist only at runtime and prefer file-based consumption.
Pros
- Secrets can be mounted as files
- Can avoid storing secrets in etcd as Kubernetes Secrets
- Supports cloud secret backends
Cons
- App needs to read from files
- Some added complexity and provider-specific setup
3. Kubernetes: Native Kubernetes Secrets
Use native Secret objects only for simpler setups or non-production cases unless you have encryption at rest, RBAC, and strong controls configured.
Recommended when: you need a straightforward native Kubernetes solution and accept the tradeoffs.
Pros
- Simple and built-in
- Easy to consume as env vars or files
Cons
- Secrets are base64-encoded, not encrypted by default
- Can be exposed via logs, env dumps, or RBAC mistakes
- Less ideal for rotation workflows
4. Docker / Compose: Runtime env or mounted files from a secret manager
If you’re not on Kubernetes, inject secrets at container start via:
- environment variables from your orchestrator/CI/CD
- mounted secret files
- Docker Swarm secrets if you use Swarm
- a sidecar/agent that fetches secrets from Vault or cloud secret manager
Recommended when: you run containers directly or with Compose.
Pros
- Simple
- Works with many deployment setups
Cons
- Environment variables are easy to leak via process inspection or logs
- Secret rotation can be clunky
What I recommend in practice
If you’re on Kubernetes:
- Best general choice: Secrets Store CSI Driver + cloud secret manager/Vault
- Best if you want sync into native K8s secrets: External Secrets Operator
If you’re on VM-based Docker or Compose:
- Use a secret manager plus runtime file injection or entrypoint fetch
- Avoid committing
.envfiles or hardcoding secrets in images
What to avoid
- Putting secrets in container images
- Passing secrets in command-line arguments
- Storing secrets in plain environment variables long-term if you can avoid it
- Checking secrets into Git
- Using config maps for secrets
Simple decision guide
- Need secrets as files in pods? Use Secrets Store CSI Driver
- Need Kubernetes-native Secret objects managed from external stores? Use External Secrets Operator
- Need the simplest possible setup? Use native Kubernetes Secrets, but harden them
- Not on Kubernetes? Use a secret manager and inject at runtime
If you tell me your environment—Kubernetes, Docker Compose, ECS, Nomad, or something else—I can recommend the best specific tool and pattern.