bake: display read definition files in build output by crazy-max · Pull Request #2076 · docker/buildx (original) (raw)

If no bake definition are specified, it will currently consume the default ones in this specified order:

names = append(names, composecli.DefaultFileNames...)
names = append(names, []string{
"docker-bake.json",
"docker-bake.override.json",
"docker-bake.hcl",
"docker-bake.override.hcl",
}...)

Following-up a discussion with @milas:

If you have both docker-bake.hcl AND docker-compose.yml in a directory, Bake always parses the Compose file unless you pass -f docker-bake.hcl explicitly
So if you have a syntax error in your compose file:

❯ docker buildx bake services
[+] Building 0.0s (0/0)                                                                                                                  cloud:cloud-stage
ERROR: parsing : yaml: line 3: mapping values are not allowed in this context

But:

❯ docker buildx bake -f docker-bake.hcl services
[+] Building 0.5s (10/10) FINISHED                                                                                                       cloud:cloud-stage
=> [registry internal] connected to docker's cloud service                                                                                           0.0s
=> => note: cloud builds are currently in beta                                                                                                       0.0s

I actually noticed this because I was getting this in my bake:

WARNING: The "BUILD_SERVICE_CLIENT_MTLS_CERT" variable is not set. Defaulting to a blank string.

and was SO confused because that's not part of the Dockerfile or Bake file and I thought it was actually coming FROM the builder but that's a variable in the Compose file (that it wasn't even using) and that's compose-go writing out that message to stdout

So there is an issue with compose-go writing on stdout warnings: https://github.com/compose-spec/compose-go/blob/2d32c3f774bda0ad20e8d06eae493047b728fc54/template/template.go#L206. Didn't find an easy way to disable logging via https://github.com/compose-spec/compose-go/blob/2d32c3f774bda0ad20e8d06eae493047b728fc54/template/template.go#L114-L116 without setting options.Interpolate.Substitute and redefining templating on our side. @milas @glours Is it possible to have smth easier to consume through loader.Options instead like options.Logging = false?

It also appears in his case that consuming a compose.yml and docker-bake.hcl when invoking docker buildx bake is unexpected/confusing. Maybe we could put some effort and clearly state this behavior in our docs https://docs.docker.com/build/bake/reference/#file-format (cc @dvdksn).

On the other hand it's pretty common to have a compose file just for runtime and a bake hcl file to build on a repository so I tend to agree with him.

This PR will show what bake definitions are consumed in the build output to mitigate this issue and help user figures out what is the source of the definition:

$ docker buildx bake binaries
[+] Building 2.1s (10/10) FINISHED                                                                                                                                                                             docker:default
 => [internal] load local bake definitions                                                                                                                                                                               0.0s 
 => => reading compose.yaml 954B / 954B                                                                                                                                                                                  0.0s
 => => reading docker-bake.hcl 3.68kB / 3.68kB                                                                                                                                                                           0.0s 
 => [internal] load .dockerignore                                                                                                                                                                                        0.1s
 => => transferring context: 45B                                                                                                                                                                                         0.0s 
 => [internal] load build definition from Dockerfile                                                                                                                                                                     0.1s 
 => => transferring dockerfile: 4.66kB                                                                                                                                                                                   0.0s 
 => resolve image config for docker.io/docker/dockerfile:1
...
$ docker buildx bake binaries
#0 building with "default" instance using docker driver

#1 [internal] load local bake definitions
#1 reading compose.yaml 954B / 954B done
#1 reading docker-bake.hcl 3.68kB / 3.68kB done
#1 DONE 0.0s

#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 4.66kB 0.0s done
#2 DONE 0.1s

#3 [internal] load .dockerignore
#3 transferring context: 45B 0.0s done
#3 DONE 0.1s

#4 resolve image config for docker.io/docker/dockerfile:1
#4 DONE 0.5s
...