Merge pull request #9847 from glours/fix-service-platform--without-build-platform

keep the platform defined at service level during build if no build patforms provided
This commit is contained in:
Guillaume Lours 2022-09-15 10:49:42 +02:00 committed by GitHub
commit b25a66bbd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 1 deletions

View File

@ -363,7 +363,12 @@ func addPlatforms(project *types.Project, service types.ServiceConfig) ([]specs.
}
if service.Platform != "" && !utils.StringContains(service.Build.Platforms, service.Platform) {
return nil, fmt.Errorf("service.platform should be part of the service.build.platforms: %q", service.Platform)
if len(service.Build.Platforms) > 0 {
return nil, fmt.Errorf("service.platform should be part of the service.build.platforms: %q", service.Platform)
}
// User defined a service platform and no build platforms, so we should keep the one define on the service level
p, err := platforms.Parse(service.Platform)
return append(plats, p), err
}
for _, buildPlatform := range service.Build.Platforms {

View File

@ -314,6 +314,14 @@ func TestBuildPlatformsWithCorrectBuildxConfig(t *testing.T) {
res.Assert(t, icmd.Expected{Out: "I am building for linux/amd64"})
assert.Assert(t, !strings.Contains(res.Stdout(), "I am building for linux/arm64"))
})
t.Run("use service platform value when no build platforms defined ", func(t *testing.T) {
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms",
"-f", "fixtures/build-test/platforms/compose-service-platform-and-no-build-platforms.yaml", "build")
assert.NilError(t, res.Error, res.Stderr())
res.Assert(t, icmd.Expected{Out: "I am building for linux/386"})
})
}
func TestBuildPlatformsStandardErrors(t *testing.T) {

View File

@ -0,0 +1,6 @@
services:
platforms:
image: build-test-platform:test
platform: linux/386
build:
context: .