Skip to content

[BUG] .env evaluated during include of a base compose file when using --env-file with different file #13128

@asuol

Description

@asuol

Description

I have a compose setup where I have a base compose file which is included by two other independent compose files, extending in different ways a service defined on the base compose file.

The service on the base compose file requires some environment variables (via required interpolation) and each of the other two compose files are meant to be used with a separate env file, both defining the variables required by the base compose service while also including some variables specific to those compose files (also with required interpolation).

The problem is that if a .env file exists then compose evaluates it and fails if one of the required interpolations fails, even though we are launching compose with another env file through --env-file.

A workaround for this issue is either:

  • Set the required variables in .env in all other env files
  • Remove any required variable interpolation from the .env file
  • Stop using .env by renaming it to something else

Steps To Reproduce

  1. Create the following files

docker-compose.base.yml

services:
  example:
    image: busybox:latest

docker-compose.dev.yml

include:
  - docker-compose.base.yml

services:
  example-dev:
    extends:
      file: docker-compose.base.yml
      service: example
    environment:
      - SOME_DEV_VAR=${SOME_DEV_VAR:?Missing SOME_DEV_VAR}

docker-compose.yml

include:
  - docker-compose.base.yml

services:
  example-prod:
    extends:
      file: docker-compose.base.yml
      service: example
    environment:
      - SOME_PROD_VAR=${SOME_PROD_VAR:?Missing SOME_PROD_VAR}

.env

SOME_PROD_VAR=${SOME_PROD_VAR:?Missing SOME_PROD_VAR in .env}

.env.dev

SOME_DEV_VAR=dummy
  1. Steps to reproduce
$  docker compose --env-file .env.dev -f docker-compose.dev.yml up
failed to read /tmp/repro/.env: required variable SOME_PROD_VAR is missing a value: Missing SOME_PROD_VAR in .env 
$ 

Compose Version

Docker Compose version 2.39.1

Docker Environment

Client:
 Version:    28.3.3
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  0.26.1
    Path:     /usr/lib/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  2.39.1
    Path:     /usr/lib/docker/cli-plugins/docker-compose

Server:
 Containers: 10
  Running: 0
  Paused: 0
  Stopped: 10
 Images: 100
 Server Version: 28.3.3
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: true
  Native Overlay Diff: false
  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
 CDI spec directories:
  /etc/cdi
  /var/run/cdi
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: c787fb98911740dd3ff2d0e45ce88cdf01410486.m
 runc version:
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.15.8-arch1-1
 Operating System: Arch Linux
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 7.675GiB
 Name: saturn
 ID: 0ae876a2-0a9c-4d7e-90d0-957ef949eeb0
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  ::1/128
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions