build_buildkit.go 2.9 KB

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