Skip to content

Commit 6794078

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 <cesar.talledo@docker.com>
1 parent 674cfff commit 6794078

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

build/build.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,6 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opts map[
525525
}
526526
}
527527
}
528-
529528
node := dp.Node().Driver
530529
if node.IsMobyDriver() {
531530
for _, e := range so.Exports {
@@ -561,6 +560,14 @@ func BuildWithResultHandler(ctx context.Context, nodes []builder.Node, opts map[
561560
}
562561
}
563562
}
563+
// if prefer-image-digest is set in the solver options, remove the image
564+
// config digest from the exporter's response
565+
for _, e := range so.Exports {
566+
if e.Attrs["prefer-image-digest"] == "true" {
567+
delete(rr.ExporterResponse, exptypes.ExporterImageConfigDigestKey)
568+
break
569+
}
570+
}
564571
return nil
565572
})
566573
}

build/opt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt *O
237237
opt.Exports[i].Output = func(_ map[string]string) (io.WriteCloser, error) {
238238
return w, nil
239239
}
240+
// if docker is using the containerd snapshotter, prefer to export the image digest
241+
// (rather than the image config digest). See https://github.com/moby/moby/issues/45458.
242+
if features[dockerutil.OCIImporter] {
243+
opt.Exports[i].Attrs["prefer-image-digest"] = "true"
244+
}
240245
}
241246
} else if !nodeDriver.Features(ctx)[driver.DockerExporter] {
242247
return nil, nil, notSupported(driver.DockerExporter, nodeDriver, "https://docs.docker.com/go/build-exporters/")

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)