Skip to content

Commit 54d9157

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 bad5063 commit 54d9157

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

commands/build.go

Lines changed: 39 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,15 @@ 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 := isDockerUsingContainerdSnapshotter(ctx, dockerCli, opts.Exports)
333+
330334
var term bool
331335
if _, err := console.ConsoleFromFile(os.Stderr); err == nil {
332336
term = true
@@ -377,12 +381,12 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
377381
case progressui.RawJSONMode:
378382
// no additional display
379383
case progressui.QuietMode:
380-
fmt.Println(getImageID(resp.ExporterResponse))
384+
fmt.Println(getImageID(resp.ExporterResponse, dockerUsingContainerdSnapshotter))
381385
default:
382386
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
383387
}
384388
if options.imageIDFile != "" {
385-
if err := os.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse)), 0644); err != nil {
389+
if err := os.WriteFile(options.imageIDFile, []byte(getImageID(resp.ExporterResponse, dockerUsingContainerdSnapshotter)), 0644); err != nil {
386390
return errors.Wrap(err, "writing image ID file")
387391
}
388392
}
@@ -412,12 +416,40 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
412416
}
413417

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

423455
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)