diff --git a/daemon/containerd/image_exporter.go b/daemon/containerd/image_exporter.go index b05aeb838986f..c3808aed0f872 100644 --- a/daemon/containerd/image_exporter.go +++ b/daemon/containerd/image_exporter.go @@ -290,6 +290,17 @@ func (i *ImageService) LoadImage(ctx context.Context, inTar io.ReadCloser, outSt return nil } + imgPlat, err := platformImg.ImagePlatform(ctx) + if err != nil { + logger.WithError(err).Warn("failed to read image platform, skipping unpack") + return nil + } + + // Only unpack the image if it matches the host platform + if !i.hostPlatformMatcher().Match(imgPlat) { + return nil + } + unpacked, err := platformImg.IsUnpacked(ctx, i.snapshotter) if err != nil { logger.WithError(err).Warn("failed to check if image is unpacked") @@ -305,12 +316,14 @@ func (i *ImageService) LoadImage(ctx context.Context, inTar io.ReadCloser, outSt logger.WithField("alreadyUnpacked", unpacked).WithError(err).Debug("unpack") return nil }) - if err != nil { - return errors.Wrap(err, "failed to unpack loaded image") - } fmt.Fprintf(progress, "%s: %s\n", loadedMsg, name) i.LogImageEvent(img.Target.Digest.String(), img.Target.Digest.String(), events.ActionLoad) + + if err != nil { + // The image failed to unpack, but is already imported, log the error but don't fail the whole load. + fmt.Fprintf(progress, "Error unpacking image %s: %v\n", name, err) + } } return nil diff --git a/daemon/containerd/platform_matchers.go b/daemon/containerd/platform_matchers.go index 43c3d02da8a9b..6e3440ac9b56f 100644 --- a/daemon/containerd/platform_matchers.go +++ b/daemon/containerd/platform_matchers.go @@ -24,3 +24,11 @@ func (c allPlatformsWithPreferenceMatcher) Match(_ ocispec.Platform) bool { func (c allPlatformsWithPreferenceMatcher) Less(p1, p2 ocispec.Platform) bool { return c.preferred.Less(p1, p2) } + +func (i *ImageService) hostPlatformMatcher() platforms.MatchComparer { + // Allow to override the host platform for testing purposes. + if i.defaultPlatformOverride != nil { + return i.defaultPlatformOverride + } + return platforms.Default() +}