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 <silvin.lubecki@docker.com>
This commit is contained in:
Silvin Lubecki 2023-08-03 21:11:16 +02:00 committed by GitHub
parent 3b0742fd57
commit 7c42776770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 13 deletions

View File

@ -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
}

View File

@ -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 {