docker-compose doesn’t load .env file when is run from outside of compose-file location

Description of the issue

In the documentation, you’ve mentioned the default project directory is the location of the docker-compose file.

When current working directory is different, docker-compose doesn’t load .env file. We have to set --project-directory.

As you can see the code below, the default directory is not the compose file location but is the current working directory :

compose/compose/cli/main.py

Lines 245 to 247
in
8633939

@property
def project_dir(self):
return self.toplevel_options.get(‘–project-directory’) or ‘.’

Context information

Output of docker-compose version

docker-compose version 1.27.4, build 40524192
docker-py version: 4.3.1
CPython version: 3.7.4
OpenSSL version: OpenSSL 1.1.1c  28 May 2019

Output of docker version

Client: Docker Engine - Community
 Cloud integration: 1.0.2
 Version:           19.03.13
 API version:       1.40
 Go version:        go1.13.15
 Git commit:        4484c46d9d
 Built:             Wed Sep 16 17:00:27 2020
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.13
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       4484c46d9d
  Built:            Wed Sep 16 17:07:04 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.3.7
  GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Output of docker-compose -f ./compose/docker-compose.yaml config

services:
  srv1:
    environment:
      MY_ENVIRONMENT: ''
    image: alpine
version: '3.1'

Steps to reproduce the issue

  1. make compose folder
  2. make docker-compose.yaml inside the compose folder
version: '3.1'
services:
  srv1:
    image: alpine
    environment:
       - MY_ENVIRONMENT=${ENVIRONMENT_NAME} 
  1. make .env inside the compose folder
ENVIRONMENT_NAME=Staging 
  1. Run docker-compose -f ./compose/docker-compose.yaml config

And to load .env correctly : docker-compose -f ./compose/docker-compose.yaml --project-directory ./compose config

Observed result

services:
  srv1:
    environment:
      MY_ENVIRONMENT: ''
    image: alpine
version: '3.1'

Expected result

services:
  srv1:
    environment:
      MY_ENVIRONMENT: 'Staging'
    image: alpine
version: '3.1'

1 possible answer(s) on “docker-compose doesn’t load .env file when is run from outside of compose-file location

  1. Hello, I help maintain a commercial project built on docker-compose.

    This change broke our current solution as we are cd‘ing to the directory which holds our .env file before calling docker-compose. Our compose files are currently held in another directory and defined via the COMPOSE_FILE environment variable.

    The compose project might consider expanding the changelog at https://docs.docker.com/compose/release-notes/ to note that the change might break downstream projects and include a note on the COMPOSE_FILE variable as well as the --file cli option.

    Thanks for all y’all do on this project!