Skip to content

Commit d01ef58

Browse files
committed
restore support for --memory
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent dec608f commit d01ef58

File tree

7 files changed

+31
-22
lines changed

7 files changed

+31
-22
lines changed

cmd/compose/build.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/compose-spec/compose-go/loader"
2727
"github.com/compose-spec/compose-go/types"
2828
buildx "github.com/docker/buildx/util/progress"
29+
cliopts "github.com/docker/cli/opts"
2930
"github.com/docker/compose/v2/pkg/progress"
3031
"github.com/docker/compose/v2/pkg/utils"
3132
"github.com/spf13/cobra"
@@ -42,7 +43,7 @@ type buildOptions struct {
4243
progress string
4344
args []string
4445
noCache bool
45-
memory string
46+
memory cliopts.MemBytes
4647
ssh string
4748
}
4849

@@ -75,17 +76,14 @@ var printerModes = []string{
7576
buildx.PrinterModeQuiet,
7677
}
7778

78-
func buildCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command {
79+
func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
7980
opts := buildOptions{
8081
ProjectOptions: p,
8182
}
8283
cmd := &cobra.Command{
8384
Use: "build [OPTIONS] [SERVICE...]",
8485
Short: "Build or rebuild services",
8586
PreRunE: Adapt(func(ctx context.Context, args []string) error {
86-
if opts.memory != "" {
87-
fmt.Fprintln(streams.Err(), "WARNING --memory is ignored as not supported in buildkit.")
88-
}
8987
if opts.quiet {
9088
opts.progress = buildx.PrinterModeQuiet
9189
devnull, err := os.Open(os.DevNull)
@@ -125,8 +123,7 @@ func buildCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *
125123
cmd.Flags().BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image")
126124
cmd.Flags().Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED")
127125
cmd.Flags().MarkHidden("no-rm") //nolint:errcheck
128-
cmd.Flags().StringVarP(&opts.memory, "memory", "m", "", "Set memory limit for the build container. Not supported on buildkit yet.")
129-
cmd.Flags().MarkHidden("memory") //nolint:errcheck
126+
cmd.Flags().VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.")
130127

131128
return cmd
132129
}
@@ -141,5 +138,7 @@ func runBuild(ctx context.Context, backend api.Service, opts buildOptions, servi
141138
if err != nil {
142139
return err
143140
}
141+
142+
apiBuildOptions.Memory = int64(opts.memory)
144143
return backend.Build(ctx, project, apiBuildOptions)
145144
}

cmd/compose/compose.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
374374
portCommand(&opts, streams, backend),
375375
imagesCommand(&opts, streams, backend),
376376
versionCommand(streams),
377-
buildCommand(&opts, streams, backend),
377+
buildCommand(&opts, backend),
378378
pushCommand(&opts, backend),
379379
pullCommand(&opts, backend),
380380
createCommand(&opts, backend),

docs/reference/compose_build.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ Build or rebuild services
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:----------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------|
10-
| `--build-arg` | `stringArray` | | Set build-time variables for services. |
11-
| `--no-cache` | | | Do not use cache when building the image |
12-
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
13-
| `--pull` | | | Always attempt to pull a newer version of the image. |
14-
| `--push` | | | Push service images. |
15-
| `-q`, `--quiet` | | | Don't print anything to STDOUT |
16-
| `--ssh` | `string` | | Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent) |
8+
| Name | Type | Default | Description |
9+
|:-----------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------|
10+
| `--build-arg` | `stringArray` | | Set build-time variables for services. |
11+
| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. |
12+
| `--no-cache` | | | Do not use cache when building the image |
13+
| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) |
14+
| `--pull` | | | Always attempt to pull a newer version of the image. |
15+
| `--push` | | | Push service images. |
16+
| `-q`, `--quiet` | | | Don't print anything to STDOUT |
17+
| `--ssh` | `string` | | Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent) |
1718

1819

1920
<!---MARKER_GEN_END-->

docs/reference/docker_compose_build.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ options:
4646
swarm: false
4747
- option: memory
4848
shorthand: m
49-
value_type: string
49+
value_type: bytes
50+
default_value: "0"
5051
description: |
51-
Set memory limit for the build container. Not supported on buildkit yet.
52+
Set memory limit for the build container. Not supported by BuildKit.
5253
deprecated: false
53-
hidden: true
54+
hidden: false
5455
experimental: false
5556
experimentalcli: false
5657
kubernetes: false

pkg/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ type BuildOptions struct {
119119
Services []string
120120
// Ssh authentications passed in the command line
121121
SSHs []types.SSHKey
122+
// Memory limit for the build container
123+
Memory int64
122124
}
123125

124126
// Apply mutates project according to build options

pkg/compose/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
9191
} else {
9292
service.Build.Args = service.Build.Args.OverrideBy(args)
9393
}
94-
id, err := s.doBuildClassic(ctx, service)
94+
id, err := s.doBuildClassic(ctx, service, options)
9595
if err != nil {
9696
return err
9797
}
@@ -102,6 +102,11 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti
102102
}
103103
return nil
104104
}
105+
106+
if options.Memory != 0 {
107+
fmt.Fprintln(s.stderr(), "WARNING: --memory is not supported by BuildKit and will be ignored.")
108+
}
109+
105110
buildOptions, err := s.toBuildOptions(project, service, options)
106111
if err != nil {
107112
return err

pkg/compose/build_classic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import (
4343
)
4444

4545
//nolint:gocyclo
46-
func (s *composeService) doBuildClassic(ctx context.Context, service types.ServiceConfig) (string, error) {
46+
func (s *composeService) doBuildClassic(ctx context.Context, service types.ServiceConfig, options api.BuildOptions) (string, error) {
4747
var (
4848
buildCtx io.ReadCloser
4949
dockerfileCtx io.ReadCloser
@@ -161,6 +161,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, service types.Servi
161161
buildOptions.Tags = append(buildOptions.Tags, service.Image)
162162
buildOptions.Dockerfile = relDockerfile
163163
buildOptions.AuthConfigs = authConfigs
164+
buildOptions.Memory = options.Memory
164165

165166
ctx, cancel := context.WithCancel(ctx)
166167
defer cancel()

0 commit comments

Comments
 (0)