build_buildkit.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "github.com/compose-spec/compose-go/types"
  18. "github.com/docker/buildx/build"
  19. "github.com/docker/buildx/driver"
  20. xprogress "github.com/docker/buildx/util/progress"
  21. )
  22. func (s *composeService) doBuildBuildkit(ctx context.Context, project *types.Project, opts map[string]build.Options, mode string) (map[string]string, error) {
  23. const drivername = "default"
  24. d, err := driver.GetDriver(ctx, drivername, nil, s.apiClient, s.configFile, nil, nil, "", nil, nil, project.WorkingDir)
  25. if err != nil {
  26. return nil, err
  27. }
  28. driverInfo := []build.DriverInfo{
  29. {
  30. Name: drivername,
  31. Driver: d,
  32. },
  33. }
  34. // Progress needs its own context that lives longer than the
  35. // build one otherwise it won't read all the messages from
  36. // build and will lock
  37. progressCtx, cancel := context.WithCancel(context.Background())
  38. defer cancel()
  39. w := xprogress.NewPrinter(progressCtx, os.Stdout, mode)
  40. // We rely on buildx "docker" builder integrated in docker engine, so don't need a DockerAPI here
  41. response, err := build.Build(ctx, driverInfo, opts, nil, nil, w)
  42. errW := w.Wait()
  43. if err == nil {
  44. err = errW
  45. }
  46. if err != nil {
  47. return nil, WrapCategorisedComposeError(err, BuildFailure)
  48. }
  49. imagesBuilt := map[string]string{}
  50. for name, img := range response {
  51. if img == nil || len(img.ExporterResponse) == 0 {
  52. continue
  53. }
  54. digest, ok := img.ExporterResponse["containerimage.digest"]
  55. if !ok {
  56. continue
  57. }
  58. imagesBuilt[name] = digest
  59. }
  60. return imagesBuilt, err
  61. }