compose icon indicating copy to clipboard operation
compose copied to clipboard

[BUG] Environment Variables Override Incorrect When Interpolation Used In --env-file

Open Efficiently-Keith opened this issue 6 months ago • 0 comments

Description

When using .env files with docker-compose via the --env-file flag, interpolated variables do not appear to resolve using values defined within the same file. Instead, interpolation seems to use variables from previously loaded .env files.

Steps To Reproduce

1. In this environment

docker-compose version
Docker Compose version v2.36.1

2. With this config

docker-compose.yml:

services:
  test-service:
    image: hello-world
    environment:
      HOSTNAME: ${ENV_HOSTNAME}
      MY_URL: ${ENV_MY_URL}

.local.env:

ENV_HOSTNAME=localhost
ENV_MY_URL="http://${ENV_HOSTNAME}"

.dev.env:

ENV_HOSTNAME=dev.my-company.com
ENV_MY_URL="http://${ENV_HOSTNAME}"

3 Run

Run

docker-compose --env-file ./.local.env --env-file ./.dev.env config

According to the docs , the values in ./.dev.env should override values in ./.local.env .

See Error / Result

This is the output

$ docker-compose --env-file ./.local.env --env-file ./.dev.env config 
name: podman-overrides-test
services:
  test-service:
    environment:
      HOSTNAME: dev.my-company.com <----------- This is correct
      MY_URL: http://localhost # <------------- This should be `http://dev.my-company.com` 
    image: hello-world
    networks:
      default: null
networks:
  default:
    name: podman-overrides-test_default

In the above case, test-service correctly getsHOSTNAME from ENV_HOSTNAME in ./.dev.env. However it's MY_URL is interpolated using the ENV_HOSTNAME value from ./.local.env , but I'd expect it to interpolate using the ENV_HOSTNAME value in ./.dev.env.

Compose Version

Docker Compose version v2.36.1

Docker Environment


Anything else?

This seems somewhat related https://github.com/docker/compose/issues/12655

Efficiently-Keith avatar Jun 03 '25 03:06 Efficiently-Keith