From f25e7a853c758eebda041f414539e082792d82a3 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 10 May 2023 10:44:12 +0200 Subject: [PATCH] check local image matches the required platform Signed-off-by: Nicolas De Loof --- pkg/compose/build.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pkg/compose/build.go b/pkg/compose/build.go index 7028bace96..feb610c6b0 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -37,6 +37,7 @@ import ( "github.com/moby/buildkit/session/sshforward/sshprovider" "github.com/moby/buildkit/util/entitlements" specs "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/pkg/errors" "github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/progress" @@ -247,9 +248,29 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ images[name] = info.ID } - for i := range project.Services { - imgName := api.GetImageNameOrDefault(project.Services[i], project.Name) + for i, service := range project.Services { + imgName := api.GetImageNameOrDefault(service, project.Name) digest, ok := images[imgName] + if service.Platform != "" { + platform, err := platforms.Parse(service.Platform) + if err != nil { + return nil, err + } + inspect, _, err := s.apiClient().ImageInspectWithRaw(ctx, digest) + if err != nil { + return nil, err + } + actual := specs.Platform{ + Architecture: inspect.Architecture, + OS: inspect.Os, + Variant: inspect.Variant, + } + if !platforms.NewMatcher(platform).Match(actual) { + return nil, errors.Errorf("image with reference %s was found but does not match the specified platform: wanted %s, actual: %s", + imgName, platforms.Format(platform), platforms.Format(actual)) + } + } + if ok { project.Services[i].CustomLabels.Add(api.ImageDigestLabel, digest) }