Skip to content

Commit b776826

Browse files
committed
check local image matches the required platform
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 3b32a26 commit b776826

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

pkg/compose/build.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/moby/buildkit/session/sshforward/sshprovider"
3838
"github.com/moby/buildkit/util/entitlements"
3939
specs "github.com/opencontainers/image-spec/specs-go/v1"
40+
"github.com/pkg/errors"
4041

4142
"github.com/docker/compose/v2/pkg/api"
4243
"github.com/docker/compose/v2/pkg/progress"
@@ -247,9 +248,29 @@ func (s *composeService) getLocalImagesDigests(ctx context.Context, project *typ
247248
images[name] = info.ID
248249
}
249250

250-
for i := range project.Services {
251-
imgName := api.GetImageNameOrDefault(project.Services[i], project.Name)
251+
for i, service := range project.Services {
252+
imgName := api.GetImageNameOrDefault(service, project.Name)
252253
digest, ok := images[imgName]
254+
if service.Platform != "" {
255+
platform, err := platforms.Parse(service.Platform)
256+
if err != nil {
257+
return nil, err
258+
}
259+
inspect, _, err := s.apiClient().ImageInspectWithRaw(ctx, digest)
260+
if err != nil {
261+
return nil, err
262+
}
263+
actual := specs.Platform{
264+
Architecture: inspect.Architecture,
265+
OS: inspect.Os,
266+
Variant: inspect.Variant,
267+
}
268+
if !platforms.NewMatcher(platform).Match(actual) {
269+
return nil, errors.Errorf("image with reference %s was found but does not match the specified platform: wanted %s, actual: %s",
270+
imgName, platforms.Format(platform), platforms.Format(actual))
271+
}
272+
}
273+
253274
if ok {
254275
project.Services[i].CustomLabels.Add(api.ImageDigestLabel, digest)
255276
}

0 commit comments

Comments
 (0)