最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to incorporate --env-file into docker-compose.yml so it doesn't have to be passed in CLI? - Stack Overflow

programmeradmin0浏览0评论

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

  1. Using env_file in docker-compose.yml → Works inside the container but doesn’t resolve variables in ports.
  2. Adding variables under environment in docker-compose.yml → Didn’t resolve the issue.
  3. 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

  1. Using env_file in docker-compose.yml → Works inside the container but doesn’t resolve variables in ports.
  2. Adding variables under environment in docker-compose.yml → Didn’t resolve the issue.
  3. Moving global.env to a root .env file → Not an option for my setup.
Share Improve this question asked Jan 30 at 15:06 Raúl SantamaríaRaúl Santamaría 932 silver badges6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The 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.

发布评论

评论列表(0)

  1. 暂无评论