Container environment with env_file
Use a service env_file to load runtime variables from a separate file. In this example worker.env sets BM13_COLOR=blue and BM13_REGION=lab. The service environment mapping replaces only BM13_COLOR with green. The region stays lab. Unlike a CLI --env-file, which supplies interpolation input, a service env_file supplies container environment. An explicitly set environment entry wins over a matching env_file entry, even if its value is empty. A one-off docker compose run -e override can change that one container without editing the service. Image ENV defaults apply where no higher-priority value is supplied.
Read the configuration
services: worker: image: busybox:1.37 pull_policy: never user: 10001:10001 command: - sh - -c - printf "%s\n" "$$BM13_COLOR" "$$BM13_REGION"; exec sleep 300 network_mode: none env_file: - ./worker.env environment: BM13_COLOR: greenManual 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/02-env-file/ in the extracted archive and run from a disposable copy of that folder.
docker compose -f compose.yaml -p bm-v13-env-file config --quietdocker compose -f compose.yaml -p bm-v13-env-file up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v13-env-file logs --tail 10 workerdocker compose -f compose.yaml -p bm-v13-env-file run --rm --no-deps -T --pull never -e BM13_COLOR=amber worker printenv BM13_COLORdocker compose -f compose.yaml -p bm-v13-env-file exec -T worker printenv BM13_COLORThe ordinary worker should log green and lab. The one-off run should print amber; a subsequent exec in the existing worker should still report green. All values are synthetic. Record the ordinary worker ID before and after the one-off run; it should be the same. To observe an edited env_file, reconcile the service with up after inspecting the resolved model; restart alone does not apply the new environment.
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-env-file downdocker compose -f compose.yaml -p bm-v13-env-file ps -aCommon mistake
Changing a file does not rewrite an existing process environment. Reconcile with up after checking the desired configuration. Do not use env files as a secret vault.
Manual practice
Add a synthetic setting to worker.env, predict whether it will be injected, and compare only that setting with exec printenv. Restore the fixture.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Container environment with env_file — Knowledge check
Reason about the Compose configuration and its consequences.
Which wins for BM13_COLOR in the normal worker?
What does --env-file select at CLI level?
Does run -e alter the existing worker?