build_buildkit.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. "os"
  17. "path/filepath"
  18. _ "github.com/docker/buildx/driver/docker" //nolint:blank-imports
  19. _ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
  20. _ "github.com/docker/buildx/driver/kubernetes" //nolint:blank-imports
  21. _ "github.com/docker/buildx/driver/remote" //nolint:blank-imports
  22. "github.com/docker/buildx/build"
  23. "github.com/docker/buildx/builder"
  24. "github.com/docker/buildx/util/dockerutil"
  25. xprogress "github.com/docker/buildx/util/progress"
  26. )
  27. func (s *composeService) doBuildBuildkit(ctx context.Context, opts map[string]build.Options, mode string) (map[string]string, error) {
  28. b, err := builder.New(s.dockerCli)
  29. if err != nil {
  30. return nil, err
  31. }
  32. nodes, err := b.LoadNodes(ctx, false)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // Progress needs its own context that lives longer than the
  37. // build one otherwise it won't read all the messages from
  38. // build and will lock
  39. progressCtx, cancel := context.WithCancel(context.Background())
  40. defer cancel()
  41. w, err := xprogress.NewPrinter(progressCtx, s.stdout(), os.Stdout, mode)
  42. if err != nil {
  43. return nil, err
  44. }
  45. response, err := build.Build(ctx, nodes, opts, dockerutil.NewClient(s.dockerCli), filepath.Dir(s.configFile().Filename), w)
  46. errW := w.Wait()
  47. if err == nil {
  48. err = errW
  49. }
  50. if err != nil {
  51. return nil, WrapCategorisedComposeError(err, BuildFailure)
  52. }
  53. imagesBuilt := map[string]string{}
  54. for name, img := range response {
  55. if img == nil || len(img.ExporterResponse) == 0 {
  56. continue
  57. }
  58. digest, ok := img.ExporterResponse["containerimage.digest"]
  59. if !ok {
  60. continue
  61. }
  62. imagesBuilt[name] = digest
  63. }
  64. return imagesBuilt, err
  65. }