build.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // +build local
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package local
  15. import (
  16. "context"
  17. "fmt"
  18. "os"
  19. "path"
  20. "strings"
  21. "github.com/docker/docker/errdefs"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/docker/buildx/build"
  24. "github.com/docker/buildx/driver"
  25. _ "github.com/docker/buildx/driver/docker" // required to get default driver registered
  26. "github.com/docker/buildx/util/progress"
  27. )
  28. func (s *composeService) ensureImagesExists(ctx context.Context, project *types.Project) error {
  29. opts := map[string]build.Options{}
  30. for _, service := range project.Services {
  31. if service.Image == "" && service.Build == nil {
  32. return fmt.Errorf("invalid service %q. Must specify either image or build", service.Name)
  33. }
  34. // TODO build vs pull should be controlled by pull policy, see https://github.com/compose-spec/compose-spec/issues/26
  35. if service.Image != "" {
  36. needPull, err := s.needPull(ctx, service)
  37. if err != nil {
  38. return err
  39. }
  40. if !needPull {
  41. continue
  42. }
  43. }
  44. if service.Build != nil {
  45. opts[service.Name] = s.toBuildOptions(service, project.WorkingDir)
  46. continue
  47. }
  48. // Buildx has no command to "just pull", see
  49. // so we bake a temporary dockerfile that will just pull and export pulled image
  50. opts[service.Name] = build.Options{
  51. Inputs: build.Inputs{
  52. ContextPath: ".",
  53. DockerfilePath: "-",
  54. InStream: strings.NewReader("FROM " + service.Image),
  55. },
  56. Tags: []string{service.Image},
  57. Pull: true,
  58. }
  59. }
  60. return s.build(ctx, project, opts)
  61. }
  62. func (s *composeService) needPull(ctx context.Context, service types.ServiceConfig) (bool, error) {
  63. _, _, err := s.apiClient.ImageInspectWithRaw(ctx, service.Image)
  64. if err != nil {
  65. if errdefs.IsNotFound(err) {
  66. return true, nil
  67. }
  68. return false, err
  69. }
  70. return false, nil
  71. }
  72. func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options) error {
  73. if len(opts) == 0 {
  74. return nil
  75. }
  76. const drivername = "default"
  77. d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, nil, nil, "", nil, project.WorkingDir)
  78. if err != nil {
  79. return err
  80. }
  81. driverInfo := []build.DriverInfo{
  82. {
  83. Name: "default",
  84. Driver: d,
  85. },
  86. }
  87. // We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
  88. w := progress.NewPrinter(ctx, os.Stdout, "auto")
  89. _, err = build.Build(ctx, driverInfo, opts, nil, nil, w)
  90. return err
  91. }
  92. func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string) build.Options {
  93. var tags []string
  94. if service.Image != "" {
  95. tags = append(tags, service.Image)
  96. }
  97. if service.Build.Dockerfile == "" {
  98. service.Build.Dockerfile = "Dockerfile"
  99. }
  100. var buildArgs map[string]string
  101. return build.Options{
  102. Inputs: build.Inputs{
  103. ContextPath: path.Join(contextPath, service.Build.Context),
  104. DockerfilePath: path.Join(contextPath, service.Build.Context, service.Build.Dockerfile),
  105. },
  106. BuildArgs: flatten(mergeArgs(service.Build.Args, buildArgs)),
  107. Tags: tags,
  108. }
  109. }
  110. func flatten(in types.MappingWithEquals) map[string]string {
  111. if len(in) == 0 {
  112. return nil
  113. }
  114. out := make(map[string]string)
  115. for k, v := range in {
  116. if v == nil {
  117. continue
  118. }
  119. out[k] = *v
  120. }
  121. return out
  122. }
  123. func mergeArgs(src types.MappingWithEquals, values map[string]string) types.MappingWithEquals {
  124. for key := range src {
  125. if val, ok := values[key]; ok {
  126. if val == "" {
  127. src[key] = nil
  128. } else {
  129. src[key] = &val
  130. }
  131. }
  132. }
  133. return src
  134. }