docker / buildx

Docker CLI plugin for extended build capabilities with BuildKit
Apache License 2.0
3.33k stars 448 forks source link

build: fix localstate for remote context and stdin #2560

Closed crazy-max closed 6 days ago

crazy-max commented 1 week ago

relates to https://github.com/docker/buildx/pull/1735 needs or closes #2561

When using a remote context or stdin the LocalPath and DockerfilePath are not correct:

$ docker buildx buildx https://github.com/docker/buildx.git
...
#14 exporting to image
#14 exporting layers done
#14 writing image sha256:d9015ff8b549fee9b8b8100cd168efea32015136c1a0a463dd5f696e130e72fa done
#14 DONE 0.0s

View build details: docker-desktop://dashboard/build/default/default/aav2ix4nw5eky66fw045dkylr

$ jq . ~/.docker/buildx/refs/default/default/aav2ix4nw5eky66fw045dkylr
{
  "Target": "default",
  "LocalPath": "/home/crazy/foo/bar/https:/github.com/docker/buildx.git",
  "DockerfilePath": ""
}
$ docker buildx build -f hello.Dockerfile https://github.com/docker/actions-toolkit.git#:__tests__/fixtures
...
#10 exporting to image
#10 exporting layers done
#10 writing image sha256:9deae3ebca280147d5096bd8858e2a008014cd6cc2e7b33e6307f06cbf11d992 done
#10 DONE 0.0s

View build details: docker-desktop://dashboard/build/default/default/7pnnqpgacnqq98oa1a1h5sz6t

$ jq . ~/.docker/buildx/refs/default/default/7pnnqpgacnqq98oa1a1h5sz6t
{
  "Target": "default",
  "LocalPath": "/home/crazy/foo/bar/https:/github.com/docker/actions-toolkit.git#:__tests__/fixtures",
  "DockerfilePath": "/home/crazy/foo/bar/hello.Dockerfile"
}
$ docker buildx build -f- . < hello.Dockerfile
...
#14 exporting to image
#14 exporting layers done
#14 writing image sha256:d9015ff8b549fee9b8b8100cd168efea32015136c1a0a463dd5f696e130e72fa done
#14 DONE 0.0s

View build details: docker-desktop://dashboard/build/default/default/aav2ix4nw5eky66fw045dkylr

$ jq . ~/.docker/buildx/refs/default/default/w38vcd5fo5cfvfyig77qjec0v
{
  "Target": "default",
  "LocalPath": "/home/crazy/hello",
  "DockerfilePath": "/home/crazy/hello/-"
}

With this change we set LocalPath and DockerfilePath accordingly if a remote context is used or stdin.

crazy-max commented 1 week ago

When checking Inputs.ContextPath, I found that it is not the same when building through controller:

$ BUILDX_EXPERIMENTAL=1 docker buildx build -f hello.Dockerfile .
...
#10 exporting to image
#10 exporting layers done
#10 writing image sha256:9deae3ebca280147d5096bd8858e2a008014cd6cc2e7b33e6307f06cbf11d992 done
#10 DONE 0.0s

View build details: docker-desktop://dashboard/build/default/default/nb6ivxlwkacfvnxach0m1vg9g

$ jq . ~/.docker/buildx/refs/default/default/nb6ivxlwkacfvnxach0m1vg9g
{
  "ContextPath": "/home/crazy/hello",
  "Target": "default",
  "LocalPath": "/home/crazy/hello",
  "DockerfilePath": "/home/crazy/hello/hello.Dockerfile"
}

Without controller:

$ docker buildx build -f hello.Dockerfile .
...
#11 exporting to image
#11 exporting layers done
#11 writing image sha256:9deae3ebca280147d5096bd8858e2a008014cd6cc2e7b33e6307f06cbf11d992 done
#11 DONE 0.0s

View build details: docker-desktop://dashboard/build/default/default/szmtd9nf4urns5872py50l9pm

$ jq . ~/.docker/buildx/refs/default/default/szmtd9nf4urns5872py50l9pm
{
  "ContextPath": ".",
  "Target": "default",
  "LocalPath": "/home/crazy/hello",
  "DockerfilePath": "/home/crazy/hello/hello.Dockerfile"
}

Seems related to https://github.com/docker/buildx/blob/04000db8da4b5a1b2c3830cfafb43b02d98c1f5c/controller/pb/path.go#L17

crazy-max commented 1 week ago

Added integration tests