From 7c4277677024c0a977423bc77cb9159d899e6d60 Mon Sep 17 00:00:00 2001 From: Silvin Lubecki <31478878+silvin-lubecki@users.noreply.github.com> Date: Thu, 3 Aug 2023 21:11:16 +0200 Subject: [PATCH] Improve buildkit node creation (#10843) Move builder and nodes initialization code up, avoiding to recreate/load them for every service build. Signed-off-by: Silvin Lubecki --- pkg/compose/build.go | 24 +++++++++++++++++++++++- pkg/compose/build_buildkit.go | 17 +++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/pkg/compose/build.go b/pkg/compose/build.go index c057af7ec..5d3f8316f 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" + "github.com/docker/buildx/builder" "github.com/docker/compose/v2/internal/tracing" "github.com/docker/buildx/controller/pb" @@ -68,6 +69,27 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti return nil, err } + // Initialize buildkit nodes + var ( + b *builder.Builder + nodes []builder.Node + ) + if buildkitEnabled { + builderName := options.Builder + if builderName == "" { + builderName = os.Getenv("BUILDX_BUILDER") + } + b, err = builder.New(s.dockerCli, builder.WithName(builderName)) + if err != nil { + return nil, err + } + + nodes, err = b.LoadNodes(ctx, false) + if err != nil { + return nil, err + } + } + // Progress needs its own context that lives longer than the // build one otherwise it won't read all the messages from // build and will lock @@ -118,7 +140,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti } buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, flatten(args)) - digest, err := s.doBuildBuildkit(ctx, service.Name, buildOptions, w, options.Builder) + digest, err := s.doBuildBuildkit(ctx, service.Name, buildOptions, w, nodes) if err != nil { return err } diff --git a/pkg/compose/build_buildkit.go b/pkg/compose/build_buildkit.go index 43bb94f8e..b530d463c 100644 --- a/pkg/compose/build_buildkit.go +++ b/pkg/compose/build_buildkit.go @@ -34,18 +34,11 @@ import ( "github.com/moby/buildkit/client" ) -func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer, builderName string) (string, error) { - b, err := builder.New(s.dockerCli, builder.WithName(builderName)) - if err != nil { - return "", err - } - - nodes, err := b.LoadNodes(ctx, false) - if err != nil { - return "", err - } - - var response map[string]*client.SolveResponse +func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer, nodes []builder.Node) (string, error) { + var ( + response map[string]*client.SolveResponse + err error + ) if s.dryRun { response = s.dryRunBuildResponse(ctx, service, opts) } else {