Environment Variables for Secrets
You'll learn why environment variables are a common but flawed approach for injecting secrets into containers, and what security risks they introduce.
The Traditional Approach
Environment variables are the simplest way to pass secrets into containers—you set them at runtime, and your application reads them using standard system calls. Many orchestration platforms like Kubernetes make this easy with built-in configuration objects.
Think of environment variables like sticky notes on your desk: convenient and visible, but anyone walking by can read them.
How It Works
When launching a container, you inject secrets as environment variables:
docker run -e DB_PASSWORD="mySecretPass123" myapp
Your application code then reads DB_PASSWORD from the environment. Simple, portable, and widely supported.
Critical Limitations and Risks
Process Inspection: Environment variables are visible to any process inspection tool (ps, /proc filesystem). Any user with access to the container can dump all environment variables—including your secrets.
Logging and Debugging: Many logging frameworks and error handlers automatically capture environment variables during crashes or debugging sessions, potentially exposing secrets in logs you never intended to persist.
Child Process Inheritance: Environment variables are inherited by all child processes, spreading secrets throughout your process tree.
No Rotation Support: Changing a secret requires restarting the container; you can't rotate credentials dynamically.
Audit Trail Gaps: Unlike dedicated secrets managers (like AWS Secrets Manager or Vault you've learned), environment variables provide no audit trail of who accessed which secret or when.
When to Use (Sparingly)
Environment variables remain acceptable for non-sensitive configuration (region names, feature flags) but should be avoided for passwords, API keys, or tokens in production environments.
Key Takeaway: Environment variables offer convenience but expose secrets to process inspection, logs, and child processes—use them only for non-sensitive configuration, never for production credentials.