Compose variables and configuration precedence
Interpolation means substituting a value while Compose reads the application model. It happens before a container starts. Here .env supplies BM13_MODE=study, and environment passes the resolved value to the worker. A shell variable of the same name overrides the value from the interpolation file. ${BM13_MODE:-safe} selects safe when the variable is missing or empty; ${BM13_MODE-safe} only defaults when missing. ${VAR:?message} rejects an absent or empty value. Use $$ to defer a dollar sign to the container shell; the explicit sh command then expands the runtime variable. A .env entry alone does not inject a container variable.
Read the configuration
services: worker: image: busybox:1.37 pull_policy: never user: 10001:10001 command: - sh - -c - printf "%s\n" "$$BM13_MODE"; exec sleep 300 network_mode: none environment: BM13_MODE: ${BM13_MODE:-safe}Manual lab
Manual lab only, not browser execution. Use a fresh approved disposable Linux VM with external egress blocked and a local Docker context. Check docker context show, docker version and docker compose version; verify support for each flag and field. Preload and verify the native-platform BusyBox 1.37 digest, entrypoint and required applets; the tag is mutable. Ensure bm-v13 project/image/volume names are unused and no inherited COMPOSE_PROFILES or BM13 variables interfere. Use only the synthetic files here, on a disposable copy. File-backed config/secret mounts are limited to these exact files; verify UID 10001 can read them. No public ports, external pulls/pushes, real secrets or host directory mounts. Run each command individually; stop on unexpected failures, perform scoped cleanup and dispose of the VM. Explicit negative probes are expected to fail. All results below are predictions. Before Engine requests, verify the effective endpoint including DOCKER_HOST and DOCKER_CONTEXT; a project name does not select the daemon. Review inherited COMPOSE_ENV_FILES, COMPOSE_DISABLE_ENV_FILE, profile and project settings. Use a clean shell and the exact fixture directory. Inspect only synthetic config, logs and files. If a build is required, verify the effective builder can resolve the approved local base without egress; stop if build fails, never reuse a stale tag.
Open lab-fixtures/docker-compose-configuration/01-interpolation/ in the extracted archive and run from a disposable copy of that folder.
docker compose -f compose.yaml -p bm-v13-interpolation configBM13_MODE=review docker compose -f compose.yaml -p bm-v13-interpolation configBM13_MODE= docker compose -f compose.yaml -p bm-v13-interpolation configdocker compose -f compose.yaml -p bm-v13-interpolation up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v13-interpolation logs --tail 10 workerdocker compose -f compose.yaml -p bm-v13-interpolation exec -T worker printenv BM13_MODEWith the supplied file and no shell override, the rendered BM13_MODE is study. With the inline shell assignment it is review; with an empty assignment it is safe. The runtime log should show study after the normal up command. These are predicted results. Record the three rendered values separately; confirm the normal invocation uses the supplied .env rather than an alternate environment file. Inspect the running worker with exec printenv to distinguish its actual environment from rendered configuration.
Clean only this disposable project. No global prune or force removal. Verify the project is empty. Retain the base image.
docker compose -f compose.yaml -p bm-v13-interpolation downdocker compose -f compose.yaml -p bm-v13-interpolation ps -aCommon mistake
Do not confuse interpolation input with container environment or embed sensitive values in a Compose command. Rendered configuration and logs can reveal them.
Manual practice
Predict the empty-value result if you remove the colon from the default expression. Compare the rendered configuration before and after the change, then restore the file.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compose variables and configuration precedence — Knowledge check
Reason about the Compose configuration and its consequences.
When does interpolation occur?
What wins over the file for BM13_MODE?
What does $$ defer in the command?