build.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. imageName := service.Image
  46. if imageName == "" {
  47. imageName = project.Name + "_" + service.Name
  48. }
  49. opts[imageName] = s.toBuildOptions(service, project.WorkingDir)
  50. continue
  51. }
  52. // Buildx has no command to "just pull", see
  53. // so we bake a temporary dockerfile that will just pull and export pulled image
  54. opts[service.Name] = build.Options{
  55. Inputs: build.Inputs{
  56. ContextPath: ".",
  57. DockerfilePath: "-",
  58. InStream: strings.NewReader("FROM " + service.Image),
  59. },
  60. Tags: []string{service.Image},
  61. Pull: true,
  62. }
  63. }
  64. return s.build(ctx, project, opts)
  65. }
  66. func (s *composeService) needPull(ctx context.Context, service types.ServiceConfig) (bool, error) {
  67. _, _, err := s.apiClient.ImageInspectWithRaw(ctx, service.Image)
  68. if err != nil {
  69. if errdefs.IsNotFound(err) {
  70. return true, nil
  71. }
  72. return false, err
  73. }
  74. return false, nil
  75. }
  76. func (s *composeService) build(ctx context.Context, project *types.Project, opts map[string]build.Options) error {
  77. if len(opts) == 0 {
  78. return nil
  79. }
  80. const drivername = "default"
  81. d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, nil, nil, "", nil, project.WorkingDir)
  82. if err != nil {
  83. return err
  84. }
  85. driverInfo := []build.DriverInfo{
  86. {
  87. Name: "default",
  88. Driver: d,
  89. },
  90. }
  91. // We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
  92. w := progress.NewPrinter(ctx, os.Stdout, "auto")
  93. _, err = build.Build(ctx, driverInfo, opts, nil, nil, w)
  94. return err
  95. }
  96. func (s *composeService) toBuildOptions(service types.ServiceConfig, contextPath string) build.Options {
  97. var tags []string
  98. if service.Image != "" {
  99. tags = append(tags, service.Image)
  100. }
  101. if service.Build.Dockerfile == "" {
  102. service.Build.Dockerfile = "Dockerfile"
  103. }
  104. var buildArgs map[string]string
  105. return build.Options{
  106. Inputs: build.Inputs{
  107. ContextPath: path.Join(contextPath, service.Build.Context),
  108. DockerfilePath: path.Join(contextPath, service.Build.Context, service.Build.Dockerfile),
  109. },
  110. BuildArgs: flatten(mergeArgs(service.Build.Args, buildArgs)),
  111. Tags: tags,
  112. }
  113. }
  114. func flatten(in types.MappingWithEquals) map[string]string {
  115. if len(in) == 0 {
  116. return nil
  117. }
  118. out := make(map[string]string)
  119. for k, v := range in {
  120. if v == nil {
  121. continue
  122. }
  123. out[k] = *v
  124. }
  125. return out
  126. }
  127. func mergeArgs(src types.MappingWithEquals, values map[string]string) types.MappingWithEquals {
  128. for key := range src {
  129. if val, ok := values[key]; ok {
  130. if val == "" {
  131. src[key] = nil
  132. } else {
  133. src[key] = &val
  134. }
  135. }
  136. }
  137. return src
  138. }