Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve buildkit node creation #10843

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
Comment on lines +78 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so we already do a look up in cmd/build.go:

builderName := opts.builder
if builderName == "" {
builderName = os.Getenv("BUILDX_BUILDER")
}

But that won't apply for up --build (while this will).

@glours Do you think that --builder should become a global flag on Compose, and let the CLI/cmd part handle the env var lookup?

[I don't think we need to resolve this as part of this PR]

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