Skip to content

Interpolation of variables in .env ignores variable precedence in V2 - was working in V1 #9442

@blacksheep--

Description

@blacksheep--

Description

The documentation is stating:

Values present in the environment at runtime always override those defined inside the .env file. Similarly, values passed via command-line arguments take precedence as well.

Interpolation in .env recently broke with that behaviour and is ignoring precendence of variables in the shell. The behaviour was introduced in V2 and was working in V1 leading to an unnecessary breaking change.

Reference: 9045

Steps to reproduce the issue:
Assume a .env of:

VERSION=from_dot_env
IMAGE=foo:${VERSION}

and a compose.yml of:

services:
  echo:
    image: bash
    environment:
      IMAGE: ${IMAGE}
    command: echo "$IMAGE"

Describe the results you received:

in V2 (using --compatibility does not make any difference):

% VERSION=from_shell docker compose config
name: test
services:
  echo:
    command:
    - bash
    - -c
    - echo foo:from_dot_env && echo from_shell
    environment:
      IMAGE: foo:from_dot_env
      VERSION: from_shell
    image: bash
    networks:
      default: null
networks:
  default:
    name: test_default

in V1:

% VERSION=from_shell docker-compose config
services:
  echo:
    command: bash -c "echo foo:from_shell && echo from_shell"
    environment:
      IMAGE: foo:from_shell
      VERSION: from_shell
    image: bash
version: '3.7'

Describe the results you expected:

I expect V2 to also return the same consistent output regarding environment variable precedence as V1 is producing:

% VERSION=from_shell docker-compose config
services:
  echo:
    command: bash -c "echo foo:from_shell && echo from_shell"
    environment:
      IMAGE: foo:from_shell
      VERSION: from_shell
    image: bash
version: '3.7'

Additional information you deem important (e.g. issue happens only occasionally):

Output of docker compose version:

Docker Compose version v2.4.1

Output of docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.8.2)
  compose: Docker Compose (Docker Inc., v2.4.1)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 3
  Running: 2
  Paused: 0
  Stopped: 1
 Images: 52
 Server Version: 20.10.14
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 3df54a852345ae127d1fa3092b95168e4a88e2f8
 runc version: v1.0.3-0-gf46b6ba
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.10.102.1-microsoft-standard-WSL2
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 9.72GiB
 Name: docker-desktop
 ID: D35Z:TDUA:SCK3:3SYM:BJL6:C3ME:ZB6G:BJ6X:O65Q:IRCD:QYOH:ZTAZ
 Docker Root Dir: /var/lib/docker
 Debug Mode: true
  File Descriptors: 60
  Goroutines: 56
  System Time: 2022-05-04T15:09:48.264070158Z
  EventsListeners: 4
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: true
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No blkio throttle.read_bps_device support
WARNING: No blkio throttle.write_bps_device support
WARNING: No blkio throttle.read_iops_device support
WARNING: No blkio throttle.write_iops_device support

Additional environment details:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions