Environment Variables and Secrets
What you'll learn: How to securely pass configuration and API keys into your containerized AI applications without hardcoding sensitive data.
The Problem with Hardcoded Secrets
Imagine baking your house key into a cake and giving that recipe to everyone. That's essentially what happens when you hardcode API keys or database passwords directly into your Dockerfile or application code. Once that image is built, those secrets are exposed to anyone with access to the container.
Three Secure Approaches
1. Environment Variables
Pass configuration at runtime using Docker's -e flag:
docker run -e OPENAI_API_KEY=sk-... -e MODEL_PATH=/models/llama my-ai-app
This keeps secrets out of your image layers and allows different values per environment (dev, staging, production).
2. Environment Files
For multiple variables, use an .env file (never commit this to Git):
docker run --env-file .env my-ai-app
3. Docker Secrets and Config Files
For production orchestration (like Docker Swarm or Kubernetes), mount secrets as read-only files:
docker run --secret my-api-key my-ai-app
Your Python application reads from /run/secrets/my-api-key instead of environment variables, providing an additional security layer.
Best Practices
- Never include secrets in Dockerfiles or commit them to version control
- Use environment variables for non-sensitive config (model paths, batch sizes)
- Use mounted secrets for sensitive data (API keys, database passwords)
- Validate that required environment variables exist at startup
- Use tools like
python-decoupleorpydantic-settingsto manage configuration centrally
Key Takeaway: Environment variables and mounted secrets let you inject configuration into containers at runtime, keeping sensitive data separate from your code and Docker images—essential for secure AI deployment.