Skip to content

Commit

Permalink
Move builder and nodes initialization code up, avoiding to recreate/l…
Browse files Browse the repository at this point in the history
…oad them for every service build.
  • Loading branch information
silvin-lubecki committed Jul 24, 2023
1 parent 8318f66 commit 961d232
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
24 changes: 23 additions & 1 deletion pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
17 changes: 5 additions & 12 deletions pkg/compose/build_buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 961d232

Please sign in to comment.