Skip to content

[BUG] short and long form includes ignore .env files #10913

@shaunco

Description

@shaunco

Description

The new include spec says:

Short Syntax:

Variables are interpolated using values set in the optional .env file in same folder, and is overridden by the local project's environment.

Long Syntax:

env_file defines an environment file(s) to use to define default values when interpolating variables in the Compose file being parsed. It defaults to .env file in the project_directory for the Compose file being parsed.
...
The local project's environment has precedence over the values set by the Compose file, so that the local project can override values for customization.

However, docker-compose seems to completely ignore both implicit and explicit .env files in includes.

Steps To Reproduce

  1. Create a directory called "postgres" that includes the following files:
    .env:

    POSTGRES_USER=postgres
    POSTGRES_PASSWORD=password
    POSTGRES_PORT=6432
    

    postgres.yml:

    services:
      postgresql:
        container_name: postgresql
        environment:
          POSTGRES_USER: ${POSTGRES_USER}
          POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
          PGPORT: ${POSTGRES_PORT}
        image: postgres:14
        ports:
          - ${POSTGRES_PORT}:${POSTGRES_PORT}
  2. Create a directory called "test" (alongside "postgres") with the following file:
    test.yml:

    include:
      - ../postgres/postgres.yml

    OR

    include:
      - path: ../postgres/postgres.yml
        project_directory: ../postgres
        env_file: ../postgres/.env
  3. From a CLI, cd into the postgres directory and run: docker-compose -f .\postgres.yml config - docker-compose will output valid config with no warnings and the three env variables specified in the .env file filled in throughout the config.

  4. From a CLI, cd into the test directory and run: docker-compose -f .\test.yml config - docker-compose will warn that it can't find any of the variables defined in the postgres .env file:

time="2023-08-18T17:08:08-07:00" level=warning msg="The \"POSTGRES_USER\" variable is not set. Defaulting to a blank string."
time="2023-08-18T17:08:08-07:00" level=warning msg="The \"POSTGRES_PASSWORD\" variable is not set. Defaulting to a blank string."
time="2023-08-18T17:08:08-07:00" level=warning msg="The \"POSTGRES_PORT\" variable is not set. Defaulting to a blank string."
time="2023-08-18T17:08:08-07:00" level=warning msg="The \"POSTGRES_PORT\" variable is not set. Defaulting to a blank string."
time="2023-08-18T17:08:08-07:00" level=warning msg="The \"POSTGRES_PORT\" variable is not set. Defaulting to a blank string."
1 error(s) decoding:

* error decoding 'ports': No port specified: :<empty>

Compose Version

Docker Compose version v2.20.2-desktop.1

Docker Environment

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions