build_buildkit.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2020 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. "context"
  16. "crypto/sha1"
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. _ "github.com/docker/buildx/driver/docker" //nolint:blank-imports
  21. _ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
  22. _ "github.com/docker/buildx/driver/kubernetes" //nolint:blank-imports
  23. _ "github.com/docker/buildx/driver/remote" //nolint:blank-imports
  24. "github.com/moby/buildkit/client"
  25. "github.com/docker/buildx/build"
  26. "github.com/docker/buildx/builder"
  27. "github.com/docker/buildx/util/dockerutil"
  28. xprogress "github.com/docker/buildx/util/progress"
  29. "github.com/docker/compose/v2/pkg/progress"
  30. )
  31. func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]build.Options, mode string) (map[string]string, error) {
  32. b, err := builder.New(s.dockerCli)
  33. if err != nil {
  34. return nil, err
  35. }
  36. nodes, err := b.LoadNodes(ctx, false)
  37. if err != nil {
  38. return nil, err
  39. }
  40. var response map[string]*client.SolveResponse
  41. if s.dryRun {
  42. response = s.dryRunBuildResponse(ctx, opts)
  43. } else {
  44. // Progress needs its own context that lives longer than the
  45. // build one otherwise it won't read all the messages from
  46. // build and will lock
  47. progressCtx, cancel := context.WithCancel(context.Background())
  48. defer cancel()
  49. w, err := xprogress.NewPrinter(progressCtx, s.stdout(), os.Stdout, mode)
  50. if err != nil {
  51. return nil, err
  52. }
  53. response, err = build.Build(ctx, nodes, opts, dockerutil.NewClient(s.dockerCli), filepath.Dir(s.configFile().Filename), w)
  54. errW := w.Wait()
  55. if err == nil {
  56. err = errW
  57. }
  58. if err != nil {
  59. return nil, WrapCategorisedComposeError(err, BuildFailure)
  60. }
  61. }
  62. imagesBuilt := map[string]string{}
  63. for name, img := range response {
  64. if img == nil || len(img.ExporterResponse) == 0 {
  65. continue
  66. }
  67. digest, ok := img.ExporterResponse["containerimage.digest"]
  68. if !ok {
  69. continue
  70. }
  71. imagesBuilt[name] = digest
  72. }
  73. return imagesBuilt, err
  74. }
  75. func (s composeService) dryRunBuildResponse(ctx context.Context, options map[string]build.Options) map[string]*client.SolveResponse {
  76. w := progress.ContextWriter(ctx)
  77. buildResponse := map[string]*client.SolveResponse{}
  78. for name, option := range options {
  79. dryRunUUID := fmt.Sprintf("dryRun-%x", sha1.Sum([]byte(name)))
  80. w.Event(progress.Event{
  81. ID: " ",
  82. Status: progress.Done,
  83. Text: fmt.Sprintf("build service %s", name),
  84. })
  85. w.Event(progress.Event{
  86. ID: "==>",
  87. Status: progress.Done,
  88. Text: fmt.Sprintf("==> writing image %s", dryRunUUID),
  89. })
  90. w.Event(progress.Event{
  91. ID: "==> ==>",
  92. Status: progress.Done,
  93. Text: fmt.Sprintf(`naming to %s`, option.Tags[0]),
  94. })
  95. buildResponse[name] = &client.SolveResponse{ExporterResponse: map[string]string{
  96. "containerimage.digest": dryRunUUID,
  97. }}
  98. }
  99. return buildResponse
  100. }