diff --git a/api/server/router/build/build_routes.go b/api/server/router/build/build_routes.go index 52ba13216d..071402ae70 100644 --- a/api/server/router/build/build_routes.go +++ b/api/server/router/build/build_routes.go @@ -24,6 +24,7 @@ import ( "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/progress" "github.com/docker/docker/pkg/streamformatter" + "github.com/docker/docker/pkg/system" "github.com/docker/go-units" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -77,6 +78,9 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui if err != nil { return nil, err } + if err := system.ValidatePlatform(sp); err != nil { + return nil, err + } options.Platform = &sp } } diff --git a/api/server/router/image/image_routes.go b/api/server/router/image/image_routes.go index 9490d7926f..85707c06d2 100644 --- a/api/server/router/image/image_routes.go +++ b/api/server/router/image/image_routes.go @@ -16,6 +16,7 @@ import ( "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/ioutils" "github.com/docker/docker/pkg/streamformatter" + "github.com/docker/docker/pkg/system" "github.com/docker/docker/registry" specs "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" @@ -49,6 +50,9 @@ func (s *imageRouter) postImagesCreate(ctx context.Context, w http.ResponseWrite if err != nil { return err } + if err := system.ValidatePlatform(sp); err != nil { + return err + } platform = &sp } } diff --git a/builder/dockerfile/dispatchers.go b/builder/dockerfile/dispatchers.go index 5372d2f0f8..1032c6cdad 100644 --- a/builder/dockerfile/dispatchers.go +++ b/builder/dockerfile/dispatchers.go @@ -165,6 +165,9 @@ func initializeStage(d dispatchRequest, cmd *instructions.Stage) error { if err != nil { return errors.Wrapf(err, "failed to parse platform %s", v) } + if err := system.ValidatePlatform(p); err != nil { + return err + } platform = &p } diff --git a/image/tarexport/load.go b/image/tarexport/load.go index 0416e28b81..786214383e 100644 --- a/image/tarexport/load.go +++ b/image/tarexport/load.go @@ -11,6 +11,7 @@ import ( "reflect" "runtime" + "github.com/containerd/containerd/platforms" "github.com/docker/distribution" "github.com/docker/distribution/reference" "github.com/docker/docker/image" @@ -421,7 +422,11 @@ func checkCompatibleOS(imageOS string) error { if runtime.GOOS != "windows" && imageOS == "windows" { return fmt.Errorf("cannot load %s image on %s", imageOS, runtime.GOOS) } - // Finally, check the image OS is supported for the platform. - // TODO(@arm64b): Leave this sanity check to the containerd code in the future - return nil + + p, err := platforms.Parse(imageOS) + if err != nil { + return err + } + + return system.ValidatePlatform(p) } diff --git a/pkg/system/lcow.go b/pkg/system/lcow.go index 630e973145..5be3e2182b 100644 --- a/pkg/system/lcow.go +++ b/pkg/system/lcow.go @@ -3,6 +3,9 @@ package system // import "github.com/docker/docker/pkg/system" import ( "runtime" "strings" + + specs "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/pkg/errors" ) // IsOSSupported determines if an operating system is supported by the host @@ -15,3 +18,15 @@ func IsOSSupported(os string) bool { } return false } + +// ValidatePlatform determines if a platform structure is valid. +// TODO This is a temporary windows-only function, should be replaced by +// comparison of worker capabilities +func ValidatePlatform(platform specs.Platform) error { + if runtime.GOOS == "windows" { + if !(platform.OS == runtime.GOOS || (LCOWSupported() && platform.OS == "linux")) { + return errors.Errorf("unsupported os %s", platform.OS) + } + } + return nil +}