options.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2023 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "fmt"
  16. "github.com/compose-spec/compose-go/v2/types"
  17. "github.com/docker/compose/v2/pkg/utils"
  18. )
  19. func applyPlatforms(project *types.Project, buildForSinglePlatform bool) error {
  20. defaultPlatform := project.Environment["DOCKER_DEFAULT_PLATFORM"]
  21. for name, service := range project.Services {
  22. if service.Build == nil {
  23. continue
  24. }
  25. // default platform only applies if the service doesn't specify
  26. if defaultPlatform != "" && service.Platform == "" {
  27. if len(service.Build.Platforms) > 0 && !utils.StringContains(service.Build.Platforms, defaultPlatform) {
  28. return fmt.Errorf("service %q build.platforms does not support value set by DOCKER_DEFAULT_PLATFORM: %s", name, defaultPlatform)
  29. }
  30. service.Platform = defaultPlatform
  31. }
  32. if service.Platform != "" {
  33. if len(service.Build.Platforms) > 0 {
  34. if !utils.StringContains(service.Build.Platforms, service.Platform) {
  35. return fmt.Errorf("service %q build configuration does not support platform: %s", name, service.Platform)
  36. }
  37. }
  38. if buildForSinglePlatform || len(service.Build.Platforms) == 0 {
  39. // if we're building for a single platform, we want to build for the platform we'll use to run the image
  40. // similarly, if no build platforms were explicitly specified, it makes sense to build for the platform
  41. // the image is designed for rather than allowing the builder to infer the platform
  42. service.Build.Platforms = []string{service.Platform}
  43. }
  44. }
  45. // services can specify that they should be built for multiple platforms, which can be used
  46. // with `docker compose build` to produce a multi-arch image
  47. // other cases, such as `up` and `run`, need a single architecture to actually run
  48. // if there is only a single platform present (which might have been inferred
  49. // from service.Platform above), it will be used, even if it requires emulation.
  50. // if there's more than one platform, then the list is cleared so that the builder
  51. // can decide.
  52. // TODO(milas): there's no validation that the platform the builder will pick is actually one
  53. // of the supported platforms from the build definition
  54. // e.g. `build.platforms: [linux/arm64, linux/amd64]` on a `linux/ppc64` machine would build
  55. // for `linux/ppc64` instead of returning an error that it's not a valid platform for the service.
  56. if buildForSinglePlatform && len(service.Build.Platforms) > 1 {
  57. // empty indicates that the builder gets to decide
  58. service.Build.Platforms = nil
  59. }
  60. project.Services[name] = service
  61. }
  62. return nil
  63. }