From 961d232f1bffa773f970afe7b0f2ab447103d0e0 Mon Sep 17 00:00:00 2001 From: Silvin Lubecki Date: Mon, 24 Jul 2023 16:09:33 +0200 Subject: [PATCH] Move builder and nodes initialization code up, avoiding to recreate/load them for every service build. --- 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 c057af7ecf..5d3f8316f6 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 43bb94f8ef..b530d463c3 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 {