Re-using environment variables in docker-compose.yml
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Re-using environment variables in a Compose file keeps container configuration consistent across services and across environments such as local development, staging, and production. It also reduces copy-and-paste errors, which is where many Compose setups become hard to maintain.
There are two related ideas to keep straight. Compose can interpolate values into the YAML file before it runs, and it can also pass environment variables into the containers themselves. Good Compose configuration uses both deliberately.
Interpolating Shared Values with .env
The most common way to re-use values is to define them once in a project-level .env file and reference them from compose.yaml. Compose resolves expressions such as ${APP_PORT} when it loads the file.
Example .env file:
Example compose.yaml:
This pattern gives you one source of truth for values reused by multiple services. A useful debugging step is:
That command prints the resolved configuration so you can confirm interpolation happened the way you expected.
Re-using the Same Environment Block
If several services need the same set of environment variables, YAML anchors can reduce repetition inside the Compose file itself.
This works well when the same block is repeated with only one or two service-specific overrides. The .env file still provides the values, while the anchor keeps the YAML readable.
When to Use env_file
env_file is different from project-level interpolation. It loads variables into the container environment from a separate file, which is useful when several services should receive the same runtime settings.
Example common.env:
This is handy when a long list of container variables should be shared unchanged. It is less useful when you need those values for Compose-level fields such as image tags, port mappings, or volume paths. For those cases, use interpolation with .env or --env-file.
Defaults, Overrides, and Escaping
Compose supports shell-style defaults, so you can provide a fallback for optional values:
You can also require a variable to be present:
If you need a literal dollar sign rather than Compose interpolation, escape it with $$:
That tells Compose not to replace the value before the container starts.
Common Pitfalls
- Confusing
.envinterpolation withenv_file. The first affects how Compose builds the configuration, while the second injects variables into containers. - Assuming every variable is available everywhere. A value used in
image:orports:must be available during interpolation, not only inside the container. - Forgetting to validate the final file.
docker compose configis the fastest way to catch missing values and bad substitutions. - Repeating large
environmentblocks by hand. Use anchors or shared files to avoid drift between services. - Accidentally interpolating a value meant for the shell inside the container. Use
$$when you need a literal dollar sign.
Summary
- Use a project
.envfile for values reused across Compose fields. - Use YAML anchors when several services share the same environment block.
- Use
env_filewhen multiple containers need the same runtime variables. - Prefer defaults such as
${VAR:-default}for optional settings. - Run
docker compose configto verify the resolved configuration before deployment.
Related reading
- Re-using existing volume with docker compose
- Read-only filesystem pod with Spring Boot application on Kubernetes
- Readiness probe for statefulset, not individual pod/container
- Recommended GCE service account authentication inside Docker container?
- react-router nginx ingress refresh causes white screen when path is not /
- readinessProbe (k8s) for kafka statefulset causes bad deployment
- Redeploy spring-boot application in docker container?
- Redirecting command output in docker

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.