I'm using Docker Compose to manage my services, and I want to load environment variables from a file without having to explicitly pass --env-file when running docker-compose up.
My Current Setup
I have an envs/ directory where I store environment variable files:
docker-compese.yml
envs/
├── global.env
├── gateway.env
My docker-compose.yml includes the env_file directive:
services:
HSM-BE-GATEWAY-MS:
image: hsm-be-gateway-ms:latest
build:
context: ../HSM-BE-GATEWAY-MS
dockerfile: Dockerfile
container_name: HSM-BE-GATEWAY-MS
env_file:
- ./envs/global.env
- ./envs/gateway.env
ports:
- "${GATEWAY_MICROSERVICE_PORT}:${GATEWAY_MICROSERVICE_PORT}"
However, when I run docker-compose up, I get warnings like this:
The "GATEWAY_MICROSERVICE_PORT" variable is not set. Defaulting to a blank string.
The "GATEWAY_MICROSERVICE_HOST" variable is not set. Defaulting to a blank string.
If I manually pass the env file via CLI like this, it works:
docker-compose --env-file envs/global.env up
What I Need
I want to avoid passing --env-file manually in the CLI every time and instead have docker-compose.yml handle it automatically.
What I’ve Tried
- Using env_file in docker-compose.yml → Works inside the container but doesn’t resolve variables in ports.
- Adding variables under environment in docker-compose.yml → Didn’t resolve the issue.
- Moving global.env to a root .env file → Not an option for my setup.
I'm using Docker Compose to manage my services, and I want to load environment variables from a file without having to explicitly pass --env-file when running docker-compose up.
My Current Setup
I have an envs/ directory where I store environment variable files:
docker-compese.yml
envs/
├── global.env
├── gateway.env
My docker-compose.yml includes the env_file directive:
services:
HSM-BE-GATEWAY-MS:
image: hsm-be-gateway-ms:latest
build:
context: ../HSM-BE-GATEWAY-MS
dockerfile: Dockerfile
container_name: HSM-BE-GATEWAY-MS
env_file:
- ./envs/global.env
- ./envs/gateway.env
ports:
- "${GATEWAY_MICROSERVICE_PORT}:${GATEWAY_MICROSERVICE_PORT}"
However, when I run docker-compose up, I get warnings like this:
The "GATEWAY_MICROSERVICE_PORT" variable is not set. Defaulting to a blank string.
The "GATEWAY_MICROSERVICE_HOST" variable is not set. Defaulting to a blank string.
If I manually pass the env file via CLI like this, it works:
docker-compose --env-file envs/global.env up
What I Need
I want to avoid passing --env-file manually in the CLI every time and instead have docker-compose.yml handle it automatically.
What I’ve Tried
- Using env_file in docker-compose.yml → Works inside the container but doesn’t resolve variables in ports.
- Adding variables under environment in docker-compose.yml → Didn’t resolve the issue.
- Moving global.env to a root .env file → Not an option for my setup.
1 Answer
Reset to default 2The Compose documentation on variable interpolation notes only three sources for variables: the shell environment (export
commands in the same shell you ran docker-compose
); .env
in the current directory; and either a --env-file
or .env
in the same directory as the Compose file. There's not an option in the Compose file to make it come from somewhere else, or to load a file for the Compose environment the same way env_file:
does for an individual container.
Most of the Compose command-line arguments can also be set from environment variables and in particular there is a COMPOSE_ENV_FILES
variable. So it might work for you to
export COMPOSE_ENV_FILES=envs/global.env,envs/gateway.env
docker-compose up -d
You do not necessarily need to specify the same files in per-container env_file:
, unless you want all of the variables to be imported with exactly the same names. You could specify individual variables or rename them in environment:
, referring to the values in the $COMPOSE_ENV_FILES
files.