Docker compose v2.6.0+ has started reading additional .env files from other locations than the first one found. 2.5.1 and earlier read the file only from the first location found.
Steps to reproduce the issue:
- Create a directory
project
- Create a subdirectory
project/sub
- In
sub create a compose.yaml file (with any services you like)
- In
sub create a .env file, with COMPOSE_FILE=compose.yaml
- In
project create a .env file with COMPOSE_FILE=sub/compose.yaml
- In
project run docker compose ps (or any other compose command)
Describe the results you received:
I get the error:
stat /home//project/compose.yaml: no such file or directory
Compose tried to read the compose file from the COMPOSE_FILE variable as it is set in the .env file in the sub directory (i.e. COMPOSE_FILE=compose.yaml, even though it has previously read the .env file from the working directory (project), and initially it did set the COMPOSE_FILE variable as it was in that file (i.e. COMPOSE_FILE=sub/compose.yaml).
Describe the results you expected:
I expected that the .env file in the location of the first COMPOSE_FILE is not read at all, as it was in 2.5.1, especially since compose has already read a .env file. Thus compose reads the .env file in the current directory, sees that it needs to read sub/compose.yaml and correctly runs the given command with that file.
Compose should not read more than one .env file.
Additional information you deem important (e.g. issue happens only occasionally):
As I wrote, this expected behaviour has been observed throughout the v2 series, until it broke in 2.6.0.
This does not just affect the COMPOSE_FILE variable, but all variables found in the .env files (e.g. COMPOSE_PROJECT_NAME etc).
The workaround is to run docker compose --env-file .env <command>. Then only the given .env file is read, the extra .env file is not read.
I use this in a project where there is an customized override file in a given directory, along with a .env file which specifies the 'main' compose file(s) as well as the extra one. My .env file looks like:
COMPOSE_PROJECT_NAME=tests-tds-$CI_PIPELINE_ID
COMPOSE_FILE=../../docker-compose.yml:../../docker-compose.dev.yml:docker-compose.yml
And ../../.env contains different settings, used for general development (e.g. COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml:docker-compose.local-sources.yml).
Output of docker compose version:
Docker Compose version v2.6.0
And also
Docker Compose version v2.6.1
Output of docker info:
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
compose: Docker Compose (Docker Inc., v2.6.1)
compose251: Docker Compose (Docker Inc., v2.5.1)
compose261: Docker Compose (Docker Inc., v2.6.1)
scan: Docker Scan (Docker Inc., v0.17.0)
Server:
Containers: 6
Running: 0
Paused: 0
Stopped: 6
Images: 4
Server Version: 20.10.17
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc version: v1.1.2-0-ga916309
init version: de40ad0
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 5.4.0-121-generic
Operating System: Ubuntu 20.04.4 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.586GiB
Name: docker-ce-ubuntu-8gb-hel1-1
ID: K7HL:KDUU:QK3Z:RUS7:TRBA:LVHY:TXKT:ZO5K:B3ZP:4KVF:7CZW:4457
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: No swap limit support
Additional environment details:
Docker compose v2.6.0+ has started reading additional .env files from other locations than the first one found. 2.5.1 and earlier read the file only from the first location found.
Steps to reproduce the issue:
projectproject/subsubcreate a compose.yaml file (with any services you like)subcreate a.envfile, withCOMPOSE_FILE=compose.yamlprojectcreate a.envfile withCOMPOSE_FILE=sub/compose.yamlprojectrundocker compose ps(or any other compose command)Describe the results you received:
I get the error:
stat /home//project/compose.yaml: no such file or directory
Compose tried to read the compose file from the
COMPOSE_FILEvariable as it is set in the.envfile in thesubdirectory (i.e.COMPOSE_FILE=compose.yaml, even though it has previously read the.envfile from the working directory (project), and initially it did set theCOMPOSE_FILEvariable as it was in that file (i.e.COMPOSE_FILE=sub/compose.yaml).Describe the results you expected:
I expected that the
.envfile in the location of the firstCOMPOSE_FILEis not read at all, as it was in 2.5.1, especially since compose has already read a.envfile. Thus compose reads the.envfile in the current directory, sees that it needs to readsub/compose.yamland correctly runs the given command with that file.Compose should not read more than one
.envfile.Additional information you deem important (e.g. issue happens only occasionally):
As I wrote, this expected behaviour has been observed throughout the v2 series, until it broke in 2.6.0.
This does not just affect the
COMPOSE_FILEvariable, but all variables found in the.envfiles (e.g.COMPOSE_PROJECT_NAMEetc).The workaround is to run
docker compose --env-file .env <command>. Then only the given.envfile is read, the extra.envfile is not read.I use this in a project where there is an customized override file in a given directory, along with a
.envfile which specifies the 'main' compose file(s) as well as the extra one. My.envfile looks like:And
../../.envcontains different settings, used for general development (e.g.COMPOSE_FILE=docker-compose.yml:docker-compose.dev.yml:docker-compose.local-sources.yml).Output of
docker compose version:And also
Output of
docker info:Additional environment details: