Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 13/ 160
01. Containerization & Docker Fundamentals

Managing Environment Variables (-e, --env-file)

Inject config via -e and env files without baking secrets into images.

Beginner-e · --env-file3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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.3 for 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

Loading interactive 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.

Concrete -e setup

docker run -d --name api --env-file app.env -e LOG_LEVEL=debug ghcr.io/acme/api:1.4.3

Key takeaway: Prove --env-file with 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.

Deliberate failure test

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.

Key takeaway: Environment variables are ubiquitous and twelve-factor friendly, but file-mounted secrets and workload identity provide stronger control for credentials.

How it works, step by step

Step 1 of 4
Design

1. 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.

Use environment variables for process configuration, and inject sensitive values from a protected runtime secret source.
Key numbers at this step
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 access in 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

Explicit -e
Best for: Inject config via -e and env files without baking secrets into images, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived --env-file experiments with low blast radius.

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.

Managed Config
Best for: Teams that centralize Config policy across many workloads.

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.3 as proof and skipping docker 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.3 address -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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Port Mapping & Exposing Services (-p, -P)
Next topic
Naming, Tagging & Labeling Containers