Skip to content

Commit

Permalink
keep the platform defined, in priority, via DOCKER_DEFAULT_PLATFORM o…
Browse files Browse the repository at this point in the history
…r the service.plaform one if no build platforms provided

Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours committed Sep 22, 2022
1 parent db88241 commit 288ee30
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *composeService) getBuildOptions(project *types.Project, images map[stri
"load": "true",
},
}}
if opt.Platforms, err = useDockerDefaultPlatform(project, service.Build.Platforms); err != nil {
if opt.Platforms, err = useDockerDefaultOrServicePlatform(project, service, true); err != nil {
opt.Platforms = []specs.Platform{}
}
opts[imageName] = opt
Expand Down Expand Up @@ -357,23 +357,11 @@ func addSecretsConfig(project *types.Project, service types.ServiceConfig) (sess
}

func addPlatforms(project *types.Project, service types.ServiceConfig) ([]specs.Platform, error) {
plats, err := useDockerDefaultPlatform(project, service.Build.Platforms)
plats, err := useDockerDefaultOrServicePlatform(project, service, false)
if err != nil {
return nil, err
}

if service.Platform != "" && !utils.StringContains(service.Build.Platforms, 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)
if !utils.Contains(plats, p) {
plats = append(plats, p)
}
return plats, err
}

for _, buildPlatform := range service.Build.Platforms {
p, err := platforms.Parse(buildPlatform)
if err != nil {
Expand Down Expand Up @@ -414,3 +402,23 @@ func useDockerDefaultPlatform(project *types.Project, platformList types.StringL
}
return plats, nil
}

func useDockerDefaultOrServicePlatform(project *types.Project, service types.ServiceConfig, useOnePlatform bool) ([]specs.Platform, error) {
plats, err := useDockerDefaultPlatform(project, service.Build.Platforms)
if (len(plats) > 0 && useOnePlatform) || err != nil {
return plats, err
}

if service.Platform != "" && !utils.StringContains(service.Build.Platforms, 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)
if !utils.Contains(plats, p) {
plats = append(plats, p)
}
return plats, err
}
return plats, nil
}

0 comments on commit 288ee30

Please sign in to comment.