Skip to content

Bake: target override using file breaks references to its tags list from other target #3173

Open

Description

@ReneNijhuis

Contributing guidelines

I've found a bug and checked that ...

  • ... the documentation does not mention anything about my problem
  • ... there are no open or closed issues that are related to my problem

Description

Error upon parsing bakefile and an override file about a targets tag list being null when referenced from another target. Occurs when the target override does not specify an overriding tag list but overrides different values.
The error occurs on the second target referencing the firsts tags but when baking only the first target its tag list is not empty.

The error only occurs when overriding using a file, using --set from the CLI correctly overrides the target without error.

Expected behaviour

The build works with the second target correctly using the referenced tag from the taglist. Using a file to override the first target should work the same as using the CLI --set flag.

Actual behaviour

An error is thrown upon parsing the bakefiles. Can be worked around by either specifying the override on the CLI or by copying the target.tags entry from the base file to the override file. When building only the first, overriden, target the tags list is not null or empty but when building the second target which references the first the error occurs.

Buildx version

github.com/docker/buildx v0.22.0-desktop.1 b45d7a53aa26a5e76aae051acdb15775c0e178da

Docker info

Client:
 Version:    28.0.4
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  ai: Docker AI Agent - Ask Gordon (Docker Inc.)
    Version:  v1.1.3
    Path:     C:\Program Files\Docker\cli-plugins\docker-ai.exe
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.22.0-desktop.1
    Path:     C:\Program Files\Docker\cli-plugins\docker-buildx.exe
  cloud: Docker Cloud (Docker Inc.)
    Version:  0.2.20
    Path:     C:\Program Files\Docker\cli-plugins\docker-cloud.exe
  compose: Docker Compose (Docker Inc.)
    Version:  v2.34.0-desktop.1
    Path:     C:\Program Files\Docker\cli-plugins\docker-compose.exe
  debug: Get a shell into any image or container (Docker Inc.)
    Version:  0.0.38
    Path:     C:\Program Files\Docker\cli-plugins\docker-debug.exe
  desktop: Docker Desktop commands (Beta) (Docker Inc.)
    Version:  v0.1.6
    Path:     C:\Program Files\Docker\cli-plugins\docker-desktop.exe
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.2
    Path:     C:\Program Files\Docker\cli-plugins\docker-dev.exe
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.27
    Path:     C:\Program Files\Docker\cli-plugins\docker-extension.exe
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v1.4.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-init.exe
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-sbom.exe
  scout: Docker Scout (Docker Inc.)
    Version:  v1.17.0
    Path:     C:\Program Files\Docker\cli-plugins\docker-scout.exe

Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 12
 Server Version: 28.0.4
 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: cgroupfs
 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 nvidia runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 753481ec61c7c8955a23d6ff7bc8e4daed455734
 runc version: v1.2.5-0-g59923ef
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
  cgroupns
 Kernel Version: 5.15.167.4-microsoft-standard-WSL2
 Operating System: Docker Desktop
 OSType: linux
 Architecture: x86_64
 CPUs: 8
 Total Memory: 7.613GiB
 Name: docker-desktop
 ID: 4b409789-cfee-4f17-a1a6-76f557128e23
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Labels:
  com.docker.desktop.address=npipe://\\.\pipe\docker_cli
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  ::1/128
  127.0.0.0/8
 Live Restore Enabled: false

Builders list

NAME/NODE           DRIVER/ENDPOINT     STATUS    BUILDKIT   PLATFORMS
default             docker
 \_ default          \_ default         running   v0.20.2    linux/amd64 (+4), linux/arm64, linux/arm (+2), linux/ppc64le, (3 more)
desktop-linux*      docker
 \_ desktop-linux    \_ desktop-linux   running   v0.20.2    linux/amd64 (+4), linux/arm64, linux/arm (+2), linux/ppc64le, (3 more)
test                                    error

Cannot load builder test: protocol not available

Configuration

The base docker-bake.hcl:

group "default" {
    targets = [ "tgt2" ]
}

target "tgt1" {
    dockerfile-inline = "FROM alpine:latest\nARG SOME_ARG\nENV IMG_VERSION=$${SOME_ARG:-1}"
    tags = ["main:latest", "primary-image:latest"]
}

target "tgt2" {
    dockerfile-inline = "FROM primary\nENV IMG_CONTINUED=true"
    contexts = { primary = "target:tgt1" }
    tags = [ target.tgt1.tags.0, "secondary-image:latest" ]
}

The override override.bake.hcl:

target "tgt1" {
    args = {
        SOME_ARG = 3
    } 
}

In the first command the override works correctly in the second it fails. Included in the log are the outputs of both since the output of the second is expected to be equal to the first.

docker buildx bake -f docker-bake.hcl --print  --set tgt1.args.SOME_ARG=3
docker buildx bake -f docker-bake.hcl -f override.bake.hcl --print

Build logs

$ ~/bake-test ❯ docker buildx bake -f docker-bake.hcl --print --set tgt1.args.SOME_ARG=3
[+] Building 0.0s (1/1) FINISHED
 => [internal] load local bake definitions                                              0.0s
 => => reading docker-bake.hcl 391B / 391B                                              0.0s
{
  "group": {
    "default": {
      "targets": [
        "tgt2"
      ]
    }
  },
  "target": {
    "tgt1": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "dockerfile-inline": "FROM alpine:latest\nARG SOME_ARG\nENV IMG_VERSION=${SOME_ARG:-1}",
      "args": {
        "SOME_ARG": "3"
      },
      "tags": [
        "main:latest",
        "primary-image:latest"
      ]
    },
    "tgt2": {
      "context": ".",
      "contexts": {
        "primary": "target:tgt1"
      },
      "dockerfile": "Dockerfile",
      "dockerfile-inline": "FROM primary\nENV IMG_CONTINUED=true",
      "tags": [
        "main:latest",
        "secondary-image:latest"
      ]
    }
  }
}

$ ~/bake-test ❯ docker buildx bake -f docker-bake.hcl -f override.bake.hcl --print
[+] Building 0.0s (1/1) FINISHED
 => [internal] load local bake definitions                                              0.0s
 => => reading docker-bake.hcl 391B / 391B                                              0.0s
 => => reading override.bake.hcl 58B / 58B                                              0.0s
docker-bake.hcl:13
--------------------
  11 |         dockerfile-inline = "FROM primary\nENV IMG_CONTINUED=true"
  12 |         contexts = { primary = "target:tgt1" }
  13 | >>>     tags = [ target.tgt1.tags.0, "secondary-image:latest" ]
  14 |     }
--------------------
ERROR: docker-bake.hcl:13,30-32: Attempt to index null value; This value is null, so it does not have any indices., and 1 other diagnostic(s)

Additional info

Occurs not only on Docker Desktop under Windows but also on Ubuntu 22.04 with Buildx version github.com/docker/buildx 0.14.1 0.14.1-0ubuntu1~22.04.1. Error is the same on both and occurs before trying to connect to the Docker socket. Provided the info of the Windows install since the error still occurs on its newer version.

Activity

  1. tonistiigi commented on May 9, 2025

    @tonistiigi
    Member

    It does work when you reverse the order of the files, but that could of course cause some other error if you change the content to a different one.

    » docker buildx bake -f override.bake.hcl -f docker-bake.hcl  --print
    [+] Building 0.0s (1/1) FINISHED
     => [internal] load local bake definitions                                                                                                                                                                                                    0.0s
     => => reading override.bake.hcl 59B / 59B                                                                                                                                                                                                    0.0s
     => => reading docker-bake.hcl 392B / 392B                                                                                                                                                                                                    0.0s
    {
      "group": {
        "default": {
          "targets": [
            "tgt2"
          ]
        }
      },
      "target": {
        "tgt1": {
          "context": ".",
          "dockerfile": "Dockerfile",
          "dockerfile-inline": "FROM alpine:latest\nARG SOME_ARG\nENV IMG_VERSION=$${SOME_ARG:-1}",
          "args": {
            "SOME_ARG": "3"
          },
          "tags": [
            "main:latest",
            "primary-image:latest"
          ],
          "output": [
            {
              "type": "cacheonly"
            }
          ]
        },
        "tgt2": {
          "context": ".",
          "contexts": {
            "primary": "target:tgt1"
          },
          "dockerfile": "Dockerfile",
          "dockerfile-inline": "FROM primary\nENV IMG_CONTINUED=true",
          "tags": [
            "main:latest",
            "secondary-image:latest"
          ]
        }
      }
    }
    
  2. ReneNijhuis commented on May 12, 2025

    @ReneNijhuis
    Author

    Sure reversing the order works in my minimal example because the base doesn't define the SOME_ARG and they are simply merged. As soon as the base does define the overriden key, then the reversed order renders the override useless since the value from the base will be the last one defined.

  3. polarathene commented on Nov 28, 2025

    @polarathene

    Just to clarify, this isn't specific to separate file inputs for overrides. You can do overrides within the same Bake config file which has it's uses.

    target "default" {
      # Shows the target context is set to the default `null` (which Bake will then fallback to `"."`):
      dockerfile = jsonencode({ for k,v in target.example: k => v if k == "context"})
    
      # Since the `context` key exists (even though it's value is `null`), the fallback won't work:
      context = try(target.example.context, "fallback")
    }
    
    VARIANTS = ["example"]
    target "matrix-gen" {
      matrix = { variant = VARIANTS }
      name = "${variant}"
      dockerfile = "Dockerfile.${variant}"
      context = variant
      # ... Other changes based on variant
      tags = ["localhost/${variant}:latest"]
    }
    
    # Override the `example` target specifically for some reason:
    # (Of course in this case you could have instead had the matrix assign with:)
    # dockerfile = variant == "example" ? "Dockerfile" : "Dockerfile.${variant}"
    target "example" {
      dockerfile = "Dockerfile"
    }

    This isn't as practical of an example, but I wanted to show that you can have target.example.context defined before the target itself was.


    Bake will resolve these before target blocks are flattened, as such the last block defined for a given target name is the one that will be queried for the value, which you can observe the default state of via jsonencode(), no merging takes place until a later step.

    Output:

    docker bake --print | yq --prettyPrint
    group:
      default:
        targets:
          - default
    target:
      default:
        context: .
        dockerfile: '{"context":null}'

    But if that last example target block was commented out, you'd get the expected context field from the matrix (doesn't have to be a matrix, I was just trying to provide a realistic example for overriding a target block in a single HCL config):

    group:
      default:
        targets:
          - default
    target:
      default:
        context: example
        dockerfile: '{"context":"example"}'

    FWIW: This same behaviour occurs for variable blocks too, and technically the undocumented global attributes (except these will error when re-declared in the same HCL config file, but can be replaced with an override file).

    A target also cannot reference itself, nor a past/future block for the same target via this syntax:

    # Either explicit or matrix target block:
    target "example" {
      tags = concat(target.example.tags, ["localhost/example:override"])
    }
    Invalid expression; reference cycle not allowed for target.example.tags
    

    You can likewise think about assigning this via an attribute, instead of used within a target block. In that sense the target to reference is ambiguous, especially if you were to think of overriding that attribute to use a different target, given how attribute overrides work.

    This is also similar for how the resolution of inherits works. You can give the default target inherits = ["example"] and it will inherit the last target block (but after that target has merged it's target blocks). The chain of inherits AFAIK would work similar to merge() would have been used to combine the blocks of a single target, except this is now across targets, establishing dependency like you can for builds with contexts that assign other targets (where it is more reliable that these are the fully resolved target).

    EDIT: Regarding attributes merge behaviour differing from variable and target blocks, I think this was discussed here which allowed merging/overrides for target blocks (and later here for attributes merging support via file overrides).


    So I'm not sure what can really be done as a way to resolve that for all contexts mentioned above?

    The behaviour that exists currently is at least consistent (not just within it's own scope, but that of similar functionality via the fields of a target), it is just not documented well.

    When you use the target.<target name> feature, it will reference the last defined block for that target, prior to the merging of configs for each target (which I assume is like calling the merge() function?).

    For the use-case of deterministic references that you can override and get the expected behaviour, using global scope attributes seems far more appropriate? (so long as there is no expectation of merging with previous assignments to the same attribute, if any)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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