How to pass environment variable to docker-compose up
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Docker Compose supports several ways to pass environment variables to containers. You can define them inline in docker-compose.yml with the environment key, load them from .env files, or pass them from the host shell. The .env file in the same directory as docker-compose.yml is automatically loaded for variable substitution in the Compose file itself, while env_file loads variables directly into the container.
Method 1: Inline in docker-compose.yml
Define variables directly under the environment key:
Or use the mapping syntax:
Method 2: .env File (Auto-loaded)
Docker Compose automatically reads a .env file in the project directory. Variables from this file are available for ${VAR} substitution in docker-compose.yml:
Method 3: Shell Environment Variables
Variables set in the host shell override .env file values:
Method 4: Inline on Command Line
Pass variables for a single command:
Method 5: env_file Directive
Load variables from a file directly into the container (different from .env substitution):
You can use multiple env files:
Method 6: docker-compose --env-file
Override the default .env file location:
Variable Precedence
When the same variable is defined in multiple places, Docker Compose uses this priority order (highest to lowest):
- Shell environment variables
.envfile (for Compose file substitution)environmentkey indocker-compose.ymlenv_filedirective- Dockerfile
ENVinstructions
Per-Environment Overrides
Use multiple Compose files for different environments:
Verifying Variables
Common Pitfalls
- Confusing
.envwithenv_file: The.envfile provides variables for${VAR}substitution indocker-compose.yml. Theenv_filedirective loads variables into the container runtime. They serve different purposes. - Unquoted values with spaces: In
.envandenv_filefiles, values with spaces do not need quotes.MY_VAR=hello worldsets the value tohello world. Adding quotes includes them literally:MY_VAR="hello"sets the value to"hello"(with quotes). - Committing secrets to
.env: Add.envto.gitignorewhen it contains passwords, API keys, or other secrets. Use Docker secrets or a vault for production credentials. - Variable substitution not working in
env_file:${VAR}syntax only works indocker-compose.yml, not insideenv_filereferenced files. Variables inenv_fileare literal — no substitution is performed. - Missing variable warning: If
${VAR}is used indocker-compose.ymlbut not set anywhere, Docker Compose substitutes an empty string and prints a warning. Use ${VAR:-default}to provide a fallback value.
Summary
- Use
environmentindocker-compose.ymlfor simple, non-sensitive variables - Use
.envfile for variable substitution in the Compose file (auto-loaded from project directory) - Use
env_fileto load variables directly into containers from external files - Shell environment variables have the highest priority and override all other sources
- Use
docker-compose configto verify resolved variable values before deploying

