Production Docker · 8 min read · Intermediate
Environment Config & Secrets Hygiene
Inject runtime configuration with --env-file and never bake API keys or .env files into Docker images.
What you'll learn
- Pass runtime config via -e and --env-file without rebuilding images
- Understand why ENV or COPY .env in a Dockerfile leaks secrets into image layers
- Separate build-time ARG from runtime ENV
A great Docker image is environment-agnostic: the exact same image runs in staging and production, with configuration (DATABASE_URL, PORT) injected at runtime via --env-file .env or Compose env_file.
Fill-in-the-Blank Exercise
Inject Runtime Env File
Pass a runtime environment file and a single override variable to docker run.
docker run -d -- .env.prod PORT=8787 devpath-api:1.0Live Env Hygiene Check Lab
Bashcheck_ignore.sh
Expected OutputClick “Run Live” to compile with GNU Bash
[PASS] .env excluded from image build context
Check Your Understanding
Score: 0 / 1
1. Why should secrets like DATABASE_URL be injected at container runtime instead of written in a Dockerfile?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.