Environment Variables, .env Files & Variable Interpolation
Pass env vars and interpolate ${VARS} from .env into compose.yaml safely.
In plain words
Validate interpolation with docker compose config so build, port and process environment do not silently receive different sources.
Think of it like…
Compose interpolation is a build-stage stencil; the resulting environment is the painted sign actually visible to the process.
Where you’ve seen it
In practice, image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} } is the concrete move for this job: pass env vars and interpolate ${VARS} from .env into compose.yaml safely. The release is not accepted until TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values confirms the result.
Words you’ll hear
- .env
- The implementation control in
image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }for pass env vars and interpolate ${VARS} from .env into compose.yaml safely. - Interpolation
- The result to observe with
TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values, rather than infer from a successful command alone. - environment
- The production boundary exposed when committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model.
Visual diagram
Theory & deep dive
.env control path
The concrete mechanism is image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }. The observable result is TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values; if those disagree, inspect the runtime evidence before changing unrelated settings.
Pass env vars and interpolate ${VARS} from .env into compose.yaml safely. Keep .env, Interpolation and environment in the same deployment record so the intent and the observed state cannot drift apart.
image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }
TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values, not with command success alone.environment failure boundary
Shell variables, .env, --env-file and service env files have distinct scopes and precedence. Quoting a dollar sign or using $$ prevents a value intended for the container shell from being expanded by Compose. The narrow failure to design for is committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model.
Accept this compromise explicitly: Interpolation is convenient and explicit with defaults; a secret manager is preferable when values require rotation and audit rather than text-file distribution. The controlled check-TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values-also identifies the point where a different design is safer.
Exercise this failure safely: committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Shell variables, .env, --env-file and service env files have distinct scopes and precedence. Quoting a dollar sign or using $$ prevents a value intended for the container shell from being expanded by Compose.
- Intent
- .env
When to use it & trade-offs
What it is
Compose performs interpolation such as ${TAG:-dev}, ${PORT:?required} and env_file before the container starts. The .env file supplies Compose values by project/model rules; a service environment: mapping supplies the resulting process variable.
Why it matters: Shell variables, .env, --env-file and service env files have distinct scopes and precedence. Quoting a dollar sign or using $$ prevents a value intended for the container shell from being expanded by Compose.
When to reach for it
- The fit is strongest for this job: pass env vars and interpolate ${VARS} from .env into compose.yaml safely. Pair it with
TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret valuesin the runbook. - Production .env changes should make Interpolation observable through
image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }, rather than trusting the runtime default. - Keep a rollback example for the case where committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model.
When to avoid or be careful
- Do not accept command success while the system can still fail by committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model.
- Avoid erasing environment protection merely to dodge this cost: “Interpolation is convenient and explicit with defaults; a secret manager is preferable when values require rotation and audit rather than text-file distribution”.
- Do not replace the evidence path-
TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values-with a screenshot or a mutable tag.
image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }: practical choices
Strengths: Compose performs interpolation such as ${TAG:-dev}, ${PORT:?required} and env_file before the container starts. The .env file supplies Compose values by project/model rules; a service environment: mapping supplies the resulting process variable.
Watch out: Interpolation is convenient and explicit with defaults; a secret manager is preferable when values require rotation and audit rather than text-file distribution.
Strengths: It can begin with image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} } and fewer surrounding controls.
Watch out: It silently accepts committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model, which makes the result hard to reproduce after promotion.
Strengths: Shell variables, .env, --env-file and service env files have distinct scopes and precedence. Quoting a dollar sign or using $$ prevents a value intended for the container shell from being expanded by Compose.
Watch out: The platform adds an ownership and debugging layer; retain TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values as the direct check.
Interview signals & numbers to know
When this topic comes up
- image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }
- TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values
- Failure signature: committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model
- Trade-off: Interpolation is convenient and explicit with defaults; a secret manager is preferable when values require rotation and audit rather than text-file distribution.
Benchmarks & rules of thumb
- 2 dollar characters
- $$ passes one literal $ into the container command
- 1 required form
- ${VAR:?message} fails the config when absent
- 0 encryption
- provided by .env
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }as proof and skippingTAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values. - Committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model
- Optimizing .env without accepting that interpolation is convenient and explicit with defaults; a secret manager is preferable when values require rotation and audit rather than text-file distribution.
Likely interviewer follow-ups
- Why does
image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }address .env here rather than through application code? - How would you distinguish healthy Interpolation from the failure “committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model” using
TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values? - When would this trade-off-“Interpolation is convenient and explicit with defaults; a secret manager is preferable when values require rotation and audit rather than text-file distribution”-justify a different environment design?
image: ghcr.io/acme/api:${TAG:-dev}; environment: { DATABASE_URL: ${DATABASE_URL:?set DATABASE_URL}, ESCAPED: $${HOST_VALUE} }-and one proof-TAG=2.1 docker compose config | sed -n /image:/p and inspect container Config.Env without printing secret values. Then name the boundary: committing a populated .env with production credentials, or troubleshooting a missing container value without checking the interpolated normalized model.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Environment Variables, .env Files & Variable Interpolation with generative AI interview Q&A and hands-on practice.