docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.62k stars 5.19k forks source link

[BUG] defining more than one env_file parameter in docker compose will result in strange behaviour #11963

Closed bastiane273 closed 2 months ago

bastiane273 commented 2 months ago

Description

If I am using more than one env_file - it does not seem to work

Steps To Reproduce

.env:

HOSTNAME=PAPERLESS

global.env

DOMAIN=xy.com

docker-compose.yml

service:
  webserver:
    env_file:
      - .env
      - global.env
    labels:
      traefik.http.routers.paperless.rule: "Host(`${PAPERLESS_HOSTNAME}.${GLOBAL_DOMAIN}`)"

an docker compose config will show traefik.http.routers.paperless.rule: "Host(paperless.})"

this

this (no env_file defined, but env-file is existing will result in traefik.http.routers.paperless.rule: "Host(paperless.})"

Compose Version

Docker Compose version v2.24.7

Docker Environment

Client: Docker Engine - Community
 Version:    25.0.4
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.13.0
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.24.7
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 106
  Running: 102
  Paused: 0
  Stopped: 4
 Images: 230
 Server Version: 25.0.4
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.5.13-1-pve
 Operating System: Debian GNU/Linux 12 (bookworm)
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 62.58GiB
 Name: servername
 ID: b9ed4556-d953-4152-bf6e-f103c8a1b7bb
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: xxxxx
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Default Address Pools:
   Base: 172.16.0.0/12, Size: 24
   Base: 172.30.0.0/16, Size: 24

Anything else?

In my opinion such a behavior makes no sense :-)

Thanks for your great software.

ndeloof commented 2 months ago

env_file is used to set container environment (same as docker run --env-file), not for interpolation. See https://docs.docker.com/compose/environment-variables/variable-interpolation/

bastiane273 commented 2 months ago

There is no interpolation I do use only more than one .env file - e.g. .env and glogal.env and .env will be treated in an other way than global.env: If I am using env_file in combination with environment - there are surprising result: e.g. .env

Var1=1

global.env

Var1=2

Result=> 1 What about the other way round docker-compose.yml

env_file:
  - global.env:
  - .env

In my opinion .env should win :-)

OK

env_file:
  - .env
  - global.env

In my opinion gobal.env should win . It doesn't Let's make a little more difficult: I want that same variable are given to the container and some not: e.g.:

env_file:
- .env
- global.env
environment:
  VARa_from_env: (works)
  VABb_from_global.env: (don't works - why)

I didn't try any combination, but it seems to be, that .env is more "worth" than global.env - without any order. Is this an intended behavior?

Greetings Bastian

ndeloof commented 2 months ago

This line traefik.http.routers.paperless.rule: "Host(${PAPERLESS_HOSTNAME}.${GLOBAL_DOMAIN})" will trigger variable interpolation if you don't double the $ sign (assuming this is Traeffic role to manage such variables)

The reason you get variables set from .env is not that you selected the right order in env_file, but .env is the default location for interpolation. env_file is not used for interpolation, but to define container's environment

bastiane273 commented 2 months ago

Is there an possibility to include an other env-file in .env?

ndeloof commented 2 months ago

no. But you can repeat --env-file flag to load multiple env files