build_buildkit.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "github.com/docker/buildx/build"
  19. "github.com/docker/buildx/builder"
  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/docker/buildx/util/confutil"
  25. "github.com/docker/buildx/util/dockerutil"
  26. buildx "github.com/docker/buildx/util/progress"
  27. "github.com/docker/compose/v2/pkg/progress"
  28. "github.com/moby/buildkit/client"
  29. )
  30. func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer, builderName string) (string, error) {
  31. b, err := builder.New(s.dockerCli, builder.WithName(builderName))
  32. if err != nil {
  33. return "", err
  34. }
  35. nodes, err := b.LoadNodes(ctx, false)
  36. if err != nil {
  37. return "", err
  38. }
  39. var response map[string]*client.SolveResponse
  40. if s.dryRun {
  41. response = s.dryRunBuildResponse(ctx, service, opts)
  42. } else {
  43. response, err = build.Build(ctx, nodes,
  44. map[string]build.Options{service: opts},
  45. dockerutil.NewClient(s.dockerCli),
  46. confutil.ConfigDir(s.dockerCli),
  47. buildx.WithPrefix(p, service, true))
  48. if err != nil {
  49. return "", WrapCategorisedComposeError(err, BuildFailure)
  50. }
  51. }
  52. for _, img := range response {
  53. if img == nil || len(img.ExporterResponse) == 0 {
  54. continue
  55. }
  56. digest, ok := img.ExporterResponse["containerimage.digest"]
  57. if !ok {
  58. continue
  59. }
  60. return digest, nil
  61. }
  62. return "", fmt.Errorf("buildkit response is missing expected result for %s", service)
  63. }
  64. func (s composeService) dryRunBuildResponse(ctx context.Context, name string, options build.Options) map[string]*client.SolveResponse {
  65. w := progress.ContextWriter(ctx)
  66. buildResponse := map[string]*client.SolveResponse{}
  67. dryRunUUID := fmt.Sprintf("dryRun-%x", sha1.Sum([]byte(name)))
  68. w.Event(progress.Event{
  69. ID: " ",
  70. Status: progress.Done,
  71. Text: fmt.Sprintf("build service %s", name),
  72. })
  73. w.Event(progress.Event{
  74. ID: "==>",
  75. Status: progress.Done,
  76. Text: fmt.Sprintf("==> writing image %s", dryRunUUID),
  77. })
  78. w.Event(progress.Event{
  79. ID: "==> ==>",
  80. Status: progress.Done,
  81. Text: fmt.Sprintf(`naming to %s`, options.Tags[0]),
  82. })
  83. buildResponse[name] = &client.SolveResponse{ExporterResponse: map[string]string{
  84. "containerimage.digest": dryRunUUID,
  85. }}
  86. return buildResponse
  87. }