Skip to content

[BUG] Docker compose build does not resolve environment vars when building when bake is enabled #12989

@blairio

Description

@blairio

Description

Given
.env

TEST_VALUE=${SYSTEM_VALUE:?system_value_not_set}

compose.yml

services:
  test:
    image: blah
    build:
      context: .
    command: "echo test value [${TEST_VALUE}]"

Dockerfile

FROM alpine:latest

When BAKE is not used (default before Docker version 28.3.0, Update Compose to v2.37.2) the following command works:

COMPOSE_BAKE=false SYSTEM_VALUE=abc  docker compose build

✔ test  Built 

or alternatively

export SYSTEM_VALUE=def
COMPOSE_BAKE=false docker compose build

With BAKE enabled, the commands fail:

COMPOSE_BAKE=true SYSTEM_VALUE=abc  docker compose build

[+] Building 0.0s (1/1) FINISHED                                                                                                                               
 => [internal] load local bake definitions                                                                                                                0.0s
 => => reading from stdin 321B                                                                                                                            0.0s
required variable SYSTEM_VALUE is missing a value: system_value_not_set

or alternatively

export SYSTEM_VALUE=def
COMPOSE_BAKE=true docker compose build

Fails.

Because the default has changed and BAKE behaves differently when loading .env files, this is now a breaking change.

Note: These commands still work as before:

COMPOSE_BAKE=true SYSTEM_VALUE=abc  docker compose up
COMPOSE_BAKE=true SYSTEM_VALUE=abc  docker compose run test

So the behaviour is not consistent with .env files between different docker compose commands.

Steps To Reproduce

  1. Use the example files above and try building the images with docker compose build

Compose Version

Docker Compose version v2.37.3

Docker Environment

Client: Docker Engine - Community
 Version:    28.3.0
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.25.0
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.37.3
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 27
  Running: 22
  Paused: 0
  Stopped: 5
 Images: 11
 Server Version: 28.3.0
 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: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 05044ec0a9a75232cad458027ca83437aae3f4da
 runc version: v1.2.5-0-g59923ef
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.1.0-26-amd64
 Operating System: Debian GNU/Linux 12 (bookworm)
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 15.62GiB
 Name: copy-show-rjs
 ID: 382ba1c3-fb45-4580-9ce1-b02b7cbc7e77
 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?

The .env file should resolve environment variables consistently in build and up and run, but in this case, when bake is enabled, it does not.

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