docker-compose 1.28.2 no longer reads .env files from the current working directory if yaml is in a different dir

Description of the issue

On 1.27.4 docker-compose would always read .env files from the current working directory.
Given the following folder structure and docker-compose.yaml file:

.
├── .env
└── subdir
    └── docker-compose.yaml
version: "3.5"

services:
  test:
    image: postgres:12.4
    ports:
      - 5432:${DB_PORT}
    environment:
      - POSTGRES_DB=${DB_DATABASE}
      - POSTGRES_USER=${DB_USERNAME}
      - POSTGRES_PASSWORD=${DB_PASSWORD}

The following used to work:

$ docker-compose -f subdir/docker-compose.yaml config
services:
  test:
    environment:
      POSTGRES_DB: db
      POSTGRES_PASSWORD: secret
      POSTGRES_USER: user
    image: postgres:12.4
    ports:
    - published: 5432
      target: 5432
version: '3.5'

On 1.28.2, this is no longer the case:

$ docker-compose -f subdir/docker-compose.yaml config
WARNING: The DB_PORT variable is not set. Defaulting to a blank string.
WARNING: The DB_DATABASE variable is not set. Defaulting to a blank string.
WARNING: The DB_USERNAME variable is not set. Defaulting to a blank string.
WARNING: The DB_PASSWORD variable is not set. Defaulting to a blank string.
ERROR: The Compose file './subdir/docker-compose.yaml' is invalid because:
services.test.ports contains an invalid type, it should be a number, or an object

If I move the .env file into subdir, it starts working again. That’s fine for the command above, but if I have compose files in different directories and do this: docker-compose -f subdir/docker-compose.yaml -f otherdir/docker-compose.yaml config I would have to copy or symlink the same .env file into multiple directories.

Context information (for bug reports)

Output of docker-compose version

docker-compose version 1.28.2, build unknown
docker-py version: 4.4.1
CPython version: 3.9.1
OpenSSL version: OpenSSL 1.1.1i  8 Dec 2020

Output of docker version

Client:
 Version:           20.10.2
 API version:       1.41
 Go version:        go1.15.6
 Git commit:        2291f610ae
 Built:             Tue Jan 19 17:19:21 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server:
 Engine:
  Version:          20.10.2
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.15.6
  Git commit:       8891c58a43
  Built:            Tue Jan 19 17:18:55 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.4.3
  GitCommit:        269548fa27e0089a8b8278fc4fc781d7f65a939b.m
 runc:
  Version:          1.0.0-rc92
  GitCommit:        ff819c7e9184c13b7c2607fe6c30ae19403a7aff
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Additional information

OS: Arch Linux x86_64
Kernel Release: 5.10.11-arch1-1

1 possible answer(s) on “docker-compose 1.28.2 no longer reads .env files from the current working directory if yaml is in a different dir

  1. I see. Using --project-directory seems to solve my problem:

    docker-compose -f subdir/docker-compose.yaml --project-directory=. config
    

    It bothers me that a minor (or patch?) release breaks current behavior, this should have been pushed to a 2.0 release where breaking changes are to be expected and stuff like this is detailed in a migration guide.

    It’s not a huge deal for me, I only have to change a few scripts etc. but still…