Managing Environment Variables (-e, --env-file)
Inject config via -e and env files without baking secrets into images.
In plain words
Use environment variables for process configuration, and inject sensitive values from a protected runtime secret source.
Think of it like…
An environment file is a labeled control-panel overlay for the same machine; a secret manager is the locked drawer that issues one key only to the operator who needs it.
Where you’ve seen it
In practice, docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3 is the concrete move for this job: inject config via -e and env files without baking secrets into images. The release is not accepted until docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access confirms the result.
Words you’ll hear
- -e
- The implementation control in
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3for inject config via -e and env files without baking secrets into images. - --env-file
- The result to observe with
docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access, rather than infer from a successful command alone. - Config
- The production boundary exposed when copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree.
Visual diagram
Theory & deep dive
-e control path
The concrete mechanism is docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3. The observable result is docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access; if those disagree, inspect the runtime evidence before changing unrelated settings.
Inject config via -e and env files without baking secrets into images. Keep -e, --env-file and Config in the same deployment record so the intent and the observed state cannot drift apart.
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3
docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access, not with command success alone.Config failure boundary
Configuration drift disappears when the same image is promoted with explicit values. Reusing the same method for passwords is unsafe because docker inspect, crash reports and child processes can reveal the values, and shell snapshots are hard to audit. The narrow failure to design for is copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree.
Accept this compromise explicitly: Environment variables are ubiquitous and twelve-factor friendly, but file-mounted secrets and workload identity provide stronger control for credentials. The controlled check-docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access-also identifies the point where a different design is safer.
Exercise this failure safely: copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Configuration drift disappears when the same image is promoted with explicit values. Reusing the same method for passwords is unsafe because docker inspect, crash reports and child processes can reveal the values, and shell snapshots are hard to audit.
- Intent
- -e
When to use it & trade-offs
What it is
Docker supplies environment variables at container creation from -e or --env-file; the values are visible in process environments and inspect metadata. Environment files provide ordinary configuration but are not a secret store or an encryption boundary.
Why it matters: Configuration drift disappears when the same image is promoted with explicit values. Reusing the same method for passwords is unsafe because docker inspect, crash reports and child processes can reveal the values, and shell snapshots are hard to audit.
When to reach for it
- The fit is strongest for this job: inject config via -e and env files without baking secrets into images. Pair it with
docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled accessin the runbook. - Production -e changes should make --env-file observable through
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3, rather than trusting the runtime default. - Keep a rollback example for the case where copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree.
When to avoid or be careful
- Do not accept command success while the system can still fail by copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree.
- Avoid erasing Config protection merely to dodge this cost: “Environment variables are ubiquitous and twelve-factor friendly, but file-mounted secrets and workload identity provide stronger control for credentials”.
- Do not replace the evidence path-
docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access-with a screenshot or a mutable tag.
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3: practical choices
Strengths: Docker supplies environment variables at container creation from -e or --env-file; the values are visible in process environments and inspect metadata. Environment files provide ordinary configuration but are not a secret store or an encryption boundary.
Watch out: Environment variables are ubiquitous and twelve-factor friendly, but file-mounted secrets and workload identity provide stronger control for credentials.
Strengths: It can begin with docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3 and fewer surrounding controls.
Watch out: It silently accepts copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree, which makes the result hard to reproduce after promotion.
Strengths: Configuration drift disappears when the same image is promoted with explicit values. Reusing the same method for passwords is unsafe because docker inspect, crash reports and child processes can reveal the values, and shell snapshots are hard to audit.
Watch out: The platform adds an ownership and debugging layer; retain docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3
- docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access
- Failure signature: copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree
- Trade-off: Environment variables are ubiquitous and twelve-factor friendly, but file-mounted secrets and workload identity provide stronger control for credentials.
Benchmarks & rules of thumb
- 1 literal process value
- the final variable available after Docker merges sources
- 12-factor fit
- configuration is portable across image deployments
- 0 encryption
- provided by a plain env file alone
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3as proof and skippingdocker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access. - Copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree
- Optimizing -e without accepting that environment variables are ubiquitous and twelve-factor friendly, but file-mounted secrets and workload identity provide stronger control for credentials.
Likely interviewer follow-ups
- Why does
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3address -e here rather than through application code? - How would you distinguish healthy --env-file from the failure “copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree” using
docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access? - When would this trade-off-“Environment variables are ubiquitous and twelve-factor friendly, but file-mounted secrets and workload identity provide stronger control for credentials”-justify a different Config design?
docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3-and one proof-docker exec api sh -c printf %s "$LOG_LEVEL" and docker inspect --format {{json .Config.Env}} api under controlled access. Then name the boundary: copying API keys into .env tracked by Git, or arguing that an image ENV value is secret because it is not in the source tree.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Managing Environment Variables (-e, --env-file) with generative AI interview Q&A and hands-on practice.