Skip to content

[BUG] extends does not properly propagate env_file #13386

@homersimpsons

Description

@homersimpsons

Description

extends and env_file (and override) does not compose well

Steps To Reproduce

Given the following files:

❯ cat .env
KEY=value
❯ cat compose.yaml 
services:
  base:
    image: scratch
    env_file: [ .env ]
    environment:
      KEY1: "value"
❯ cat compose.override.yaml 
services:
  base:
    image: scratch
  ext:
    extends: base

I would expect (+) the following docker compose config instead of the received (-) one (here, the ext service has no environment while I would expect it to inherit them):

❯ docker compose config
name: abc
services:
  base:
    environment:
      KEY: value
      KEY1: value
    image: scratch
    networks:
      default: null
  ext:
+    environment:
+      KEY: value
+      KEY1: value
    image: scratch
    networks:
      default: null
networks:
  default:
    name: abc_default

Compose Version

Docker Compose version v2.40.3

Docker Environment

Client: Docker Engine - Community
 Version:    28.5.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.29.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.40.3
    Path:     /usr/libexec/docker/cli-plugins/docker-compose
  scout: Docker Scout (Docker Inc.)
    Version:  v1.15.0
    Path:     /home/user/.docker/cli-plugins/docker-scout

Server:
 Containers: 14
  Running: 13
  Paused: 0
  Stopped: 1
 Images: 75
 Server Version: 28.5.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  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: runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: b98a3aace656320842a23f4a392a33f46af97866
 runc version: v1.3.0-0-g4ca628d1
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.14.0-35-generic
 Operating System: Ubuntu 24.04.3 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 20
 Total Memory: 30.96GiB
 Name: guillaume-Precision-5680
 ID: ae2afd81-5db7-412c-ac52-d04e14d381a6
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: homersimpsons8
 Experimental: false
 Insecure Registries:
  ::1/128
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

Interestingly, I tried various variations of this.

environment override

❯ cat .env
KEY=value
❯ cat compose.yaml 
services:
  base:
    image: scratch
    env_file: [ .env ]
    environment:
      KEY1: "value"
❯ cat compose.override.yaml 
services:
  base:
    image: scratch
    environment:
      KEY1: "valueoverride"
  ext:
    extends: base
name: abc
services:
  base:
    environment:
      KEY: value
      KEY1: valueoverride
    image: scratch
    networks:
      default: null
  ext:
    environment:
+      KEY: value
      KEY1: valueoverride
    image: scratch
    networks:
      default: null
networks:
  default:
    name: abc_default

env_file re-declaration

❯ cat .env
KEY=value
❯ cat compose.yaml 
services:
  base:
    image: scratch
    env_file: [ .env ]
    environment:
      KEY1: "value"
❯ cat compose.override.yaml 
services:
  base:
    image: scratch
    env_file: [ .env ]
  ext:
    extends: base
❯ docker compose config
name: abc
services:
  base:
    environment:
      KEY: value
      KEY1: value
    image: scratch
    networks:
      default: null
  ext:
    environment:
      KEY: value
+      KEY1: value
    image: scratch
    networks:
      default: null
networks:
  default:
    name: abc_default

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions