Skip to content

Commit a18133c

Browse files
committed
Output correct image ID when using Docker with the containerd-snapshotter.
Prior to this change, the following command emits the wrong image ID when buildx uses the "docker-container" driver and Docker is configured with the containerd-snapshotter. $ docker buildx build --load --iidfile=img.txt $ docker run --rm "$(cat img.txt)" echo hello docker: Error response from daemon: No such image: sha256:4ac37e81e00f242010e42f3251094e47de6100e01d25e9bd0feac6b8906976df. See 'docker run --help'. The problem is that buildx is outputing the incorrect image ID in this scenario (it's outputing the container image config digest, instead of the container image digest used by the containerd-snapshotter). This commit fixes this. See moby/moby#45458. Signed-off-by: Cesar Talledo <[email protected]>
1 parent d69301d commit a18133c

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

commands/build.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/docker/buildx/util/cobrautil"
3434
"github.com/docker/buildx/util/confutil"
3535
"github.com/docker/buildx/util/desktop"
36+
"github.com/docker/buildx/util/dockerutil"
3637
"github.com/docker/buildx/util/ioset"
3738
"github.com/docker/buildx/util/metricutil"
3839
"github.com/docker/buildx/util/osutil"
@@ -321,12 +322,18 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
321322
if err != nil {
322323
return err
323324
}
325+
324326
_, err = b.LoadNodes(ctx)
325327
if err != nil {
326328
return err
327329
}
328330
driverType := b.Driver
329331

332+
dockerUsingContainerdSnapshotter := false
333+
if driverType == "docker" || driverType == "docker-container" {
334+
dockerUsingContainerdSnapshotter = isDockerUsingContainerdSnapshotter(ctx, dockerCli, opts.Exports)
335+
}
336+
330337
var term bool
331338
if _, err := console.ConsoleFromFile(os.Stderr); err == nil {
332339
term = true
@@ -377,12 +384,12 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
377384
case progressui.RawJSONMode:
378385
// no additional display
379386
case progressui.QuietMode:
380-
fmt.Println(getImageID(resp.ExporterResponse))
387+
fmt.Println(getImageID(resp.ExporterResponse, dockerUsingContainerdSnapshotter))
381388
default:
382389
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
383390
}
384391
if options.imageIDFile != "" {
385-
if err := os.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse)), 0644); err != nil {
392+
if err := os.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse, dockerUsingContainerdSnapshotter)), 0644); err != nil {
386393
return errors.Wrap(err, "writing image ID file")
387394
}
388395
}
@@ -408,12 +415,40 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
408415
}
409416

410417
// getImageID returns the image ID - the digest of the image config
411-
func getImageID(resp map[string]string) string {
412-
dgst := resp[exptypes.ExporterImageDigestKey]
413-
if v, ok := resp[exptypes.ExporterImageConfigDigestKey]; ok {
414-
dgst = v
418+
func getImageID(resp map[string]string, dockerUsingContainerdSnapshotter bool) string {
419+
if dockerUsingContainerdSnapshotter {
420+
// If Docker is using the containerd snapshotter, use the image digest, not
421+
// the image config digest. See https://github.com/moby/moby/issues/45458.
422+
return resp[exptypes.ExporterImageDigestKey]
423+
} else {
424+
return resp[exptypes.ExporterImageConfigDigestKey]
415425
}
416-
return dgst
426+
}
427+
428+
func isDockerUsingContainerdSnapshotter(ctx context.Context, dockerCli command.Cli, exports []*controllerapi.ExportEntry) bool {
429+
usingContainerdSnapshotter := false
430+
431+
// If the build command has "-o type=docker,context=<mycontext>", then pass that
432+
// context to docker.Features() below, so we can find out if the associated
433+
// Docker engine is using the containerd snapshotter.
434+
dockerCtx := ""
435+
for _, e := range exports {
436+
if e.Type == "docker" {
437+
for k, v := range e.Attrs {
438+
if k == "context" {
439+
dockerCtx = v
440+
}
441+
}
442+
}
443+
}
444+
445+
docker := dockerutil.NewClient(dockerCli)
446+
features := docker.Features(ctx, dockerCtx)
447+
if features[dockerutil.OCIImporter] {
448+
usingContainerdSnapshotter = true
449+
}
450+
451+
return usingContainerdSnapshotter
417452
}
418453

419454
func runBasicBuild(ctx context.Context, dockerCli command.Cli, opts *controllerapi.BuildOptions, printer *progress.Printer) (*client.SolveResponse, *build.Inputs, error) {

tests/build.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,25 @@ func testImageIDOutput(t *testing.T, sb integration.Sandbox) {
399399

400400
require.Equal(t, dgst.String(), strings.TrimSpace(stdout.String()))
401401

402+
// read the md.json file
402403
dt, err = os.ReadFile(filepath.Join(targetDir, "md.json"))
403404
require.NoError(t, err)
404405

405406
type mdT struct {
407+
Digest string `json:"containerimage.digest"`
406408
ConfigDigest string `json:"containerimage.config.digest"`
407409
}
410+
408411
var md mdT
409412
err = json.Unmarshal(dt, &md)
410413
require.NoError(t, err)
411414

412415
require.NotEmpty(t, md.ConfigDigest)
413-
require.Equal(t, dgst, digest.Digest(md.ConfigDigest))
416+
require.NotEmpty(t, md.Digest)
417+
418+
// verify the image ID output is correct
419+
// XXX: improve this by checking that it's one of the two expected digests depending on the scenario.
420+
require.Contains(t, []digest.Digest{digest.Digest(md.ConfigDigest), digest.Digest(md.Digest)}, dgst)
414421
}
415422

416423
func testBuildMobyFromLocalImage(t *testing.T, sb integration.Sandbox) {

0 commit comments

Comments
 (0)