Skip to content

Commit 827e864

Browse files
authored
Merge pull request docker#10745 from glours/add-builder-support
add support of --builder and BUILDX_BUILDER
2 parents fa3e16c + 28301fb commit 827e864

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

cmd/compose/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type buildOptions struct {
4343
noCache bool
4444
memory cliopts.MemBytes
4545
ssh string
46+
builder string
4647
}
4748

4849
func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions, error) {
@@ -54,6 +55,10 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
5455
return api.BuildOptions{}, err
5556
}
5657
}
58+
builderName := opts.builder
59+
if builderName == "" {
60+
builderName = os.Getenv("BUILDX_BUILDER")
61+
}
5762

5863
return api.BuildOptions{
5964
Pull: opts.pull,
@@ -64,6 +69,7 @@ func (opts buildOptions) toAPIBuildOptions(services []string) (api.BuildOptions,
6469
Quiet: opts.quiet,
6570
Services: services,
6671
SSHs: SSHKeys,
72+
Builder: builderName,
6773
}, nil
6874
}
6975

@@ -101,6 +107,7 @@ func buildCommand(p *ProjectOptions, progress *string, backend api.Service) *cob
101107
cmd.Flags().BoolVar(&opts.pull, "pull", false, "Always attempt to pull a newer version of the image.")
102108
cmd.Flags().StringArrayVar(&opts.args, "build-arg", []string{}, "Set build-time variables for services.")
103109
cmd.Flags().StringVar(&opts.ssh, "ssh", "", "Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent)")
110+
cmd.Flags().StringVar(&opts.builder, "builder", "", "Set builder to use.")
104111
cmd.Flags().Bool("parallel", true, "Build images in parallel. DEPRECATED")
105112
cmd.Flags().MarkHidden("parallel") //nolint:errcheck
106113
cmd.Flags().Bool("compress", true, "Compress the build context using gzip. DEPRECATED")

docs/reference/compose_build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Build or rebuild services
88
| Name | Type | Default | Description |
99
|:-----------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------|
1010
| `--build-arg` | `stringArray` | | Set build-time variables for services. |
11+
| `--builder` | `string` | | Set builder to use. |
1112
| `--dry-run` | | | Execute command in dry run mode |
1213
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
1314
| `--no-cache` | | | Do not use cache when building the image |

docs/reference/docker_compose_build.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ options:
2424
experimentalcli: false
2525
kubernetes: false
2626
swarm: false
27+
- option: builder
28+
value_type: string
29+
description: Set builder to use.
30+
deprecated: false
31+
hidden: false
32+
experimental: false
33+
experimentalcli: false
34+
kubernetes: false
35+
swarm: false
2736
- option: compress
2837
value_type: bool
2938
default_value: "true"

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ type BuildOptions struct {
130130
SSHs []types.SSHKey
131131
// Memory limit for the build container
132132
Memory int64
133+
// Builder name passed in the command line
134+
Builder string
133135
}
134136

135137
// Apply mutates project according to build options

pkg/compose/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
116116
}
117117
buildOptions.BuildArgs = mergeArgs(buildOptions.BuildArgs, flatten(args))
118118

119-
digest, err := s.doBuildBuildkit(ctx, service.Name, buildOptions, w)
119+
digest, err := s.doBuildBuildkit(ctx, service.Name, buildOptions, w, options.Builder)
120120
if err != nil {
121121
return err
122122
}

pkg/compose/build_buildkit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
"github.com/moby/buildkit/client"
3535
)
3636

37-
func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer) (string, error) {
38-
b, err := builder.New(s.dockerCli)
37+
func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer, builderName string) (string, error) {
38+
b, err := builder.New(s.dockerCli, builder.WithName(builderName))
3939
if err != nil {
4040
return "", err
4141
}

pkg/e2e/build_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package e2e
1818

1919
import (
20+
"fmt"
2021
"net/http"
2122
"runtime"
2223
"strings"
@@ -423,3 +424,30 @@ func TestBuildPlatformsStandardErrors(t *testing.T) {
423424
})
424425

425426
}
427+
428+
func TestBuildBuilder(t *testing.T) {
429+
c := NewParallelCLI(t)
430+
builderName := "build-with-builder"
431+
// declare builder
432+
result := c.RunDockerCmd(t, "buildx", "create", "--name", builderName, "--use", "--bootstrap")
433+
assert.NilError(t, result.Error)
434+
435+
t.Cleanup(func() {
436+
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test/", "down")
437+
_ = c.RunDockerCmd(t, "buildx", "rm", "-f", builderName)
438+
})
439+
440+
t.Run("use specific builder to run build command", func(t *testing.T) {
441+
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test", "build", "--builder", builderName)
442+
assert.NilError(t, res.Error, res.Stderr())
443+
})
444+
445+
t.Run("error when using specific builder to run build command", func(t *testing.T) {
446+
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test", "build", "--builder", "unknown-builder")
447+
res.Assert(t, icmd.Expected{
448+
ExitCode: 1,
449+
Err: fmt.Sprintf(`no builder %q found`, "unknown-builder"),
450+
})
451+
})
452+
453+
}

0 commit comments

Comments
 (0)