build_buildkit.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "path/filepath"
  19. _ "github.com/docker/buildx/driver/docker" //nolint:blank-imports
  20. _ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
  21. _ "github.com/docker/buildx/driver/kubernetes" //nolint:blank-imports
  22. _ "github.com/docker/buildx/driver/remote" //nolint:blank-imports
  23. buildx "github.com/docker/buildx/util/progress"
  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. "github.com/docker/compose/v2/pkg/progress"
  29. )
  30. func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer) (map[string]string, error) {
  31. b, err := builder.New(s.dockerCli)
  32. if err != nil {
  33. return nil, err
  34. }
  35. nodes, err := b.LoadNodes(ctx, false)
  36. if err != nil {
  37. return nil, 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, map[string]build.Options{service: opts}, dockerutil.NewClient(s.dockerCli), filepath.Dir(s.configFile().Filename), buildx.WithPrefix(p, service, true))
  44. if err != nil {
  45. return nil, WrapCategorisedComposeError(err, BuildFailure)
  46. }
  47. }
  48. imagesBuilt := map[string]string{}
  49. for name, img := range response {
  50. if img == nil || len(img.ExporterResponse) == 0 {
  51. continue
  52. }
  53. digest, ok := img.ExporterResponse["containerimage.digest"]
  54. if !ok {
  55. continue
  56. }
  57. imagesBuilt[name] = digest
  58. }
  59. return imagesBuilt, err
  60. }
  61. func (s composeService) dryRunBuildResponse(ctx context.Context, name string, options build.Options) map[string]*client.SolveResponse {
  62. w := progress.ContextWriter(ctx)
  63. buildResponse := map[string]*client.SolveResponse{}
  64. dryRunUUID := fmt.Sprintf("dryRun-%x", sha1.Sum([]byte(name)))
  65. w.Event(progress.Event{
  66. ID: " ",
  67. Status: progress.Done,
  68. Text: fmt.Sprintf("build service %s", name),
  69. })
  70. w.Event(progress.Event{
  71. ID: "==>",
  72. Status: progress.Done,
  73. Text: fmt.Sprintf("==> writing image %s", dryRunUUID),
  74. })
  75. w.Event(progress.Event{
  76. ID: "==> ==>",
  77. Status: progress.Done,
  78. Text: fmt.Sprintf(`naming to %s`, options.Tags[0]),
  79. })
  80. buildResponse[name] = &client.SolveResponse{ExporterResponse: map[string]string{
  81. "containerimage.digest": dryRunUUID,
  82. }}
  83. return buildResponse
  84. }