diff --git a/cmd/compose/build.go b/cmd/compose/build.go index d17d34ef65..e8b2db40d3 100644 --- a/cmd/compose/build.go +++ b/cmd/compose/build.go @@ -26,6 +26,7 @@ import ( "github.com/compose-spec/compose-go/loader" "github.com/compose-spec/compose-go/types" buildx "github.com/docker/buildx/util/progress" + cliopts "github.com/docker/cli/opts" "github.com/docker/compose/v2/pkg/progress" "github.com/docker/compose/v2/pkg/utils" "github.com/spf13/cobra" @@ -42,7 +43,7 @@ type buildOptions struct { progress string args []string noCache bool - memory string + memory cliopts.MemBytes ssh string } @@ -75,7 +76,7 @@ var printerModes = []string{ buildx.PrinterModeQuiet, } -func buildCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cobra.Command { +func buildCommand(p *ProjectOptions, backend api.Service) *cobra.Command { opts := buildOptions{ ProjectOptions: p, } @@ -83,9 +84,6 @@ func buildCommand(p *ProjectOptions, streams api.Streams, backend api.Service) * Use: "build [OPTIONS] [SERVICE...]", Short: "Build or rebuild services", PreRunE: Adapt(func(ctx context.Context, args []string) error { - if opts.memory != "" { - fmt.Fprintln(streams.Err(), "WARNING --memory is ignored as not supported in buildkit.") - } if opts.quiet { opts.progress = buildx.PrinterModeQuiet devnull, err := os.Open(os.DevNull) @@ -125,8 +123,7 @@ func buildCommand(p *ProjectOptions, streams api.Streams, backend api.Service) * cmd.Flags().BoolVar(&opts.noCache, "no-cache", false, "Do not use cache when building the image") cmd.Flags().Bool("no-rm", false, "Do not remove intermediate containers after a successful build. DEPRECATED") cmd.Flags().MarkHidden("no-rm") //nolint:errcheck - cmd.Flags().StringVarP(&opts.memory, "memory", "m", "", "Set memory limit for the build container. Not supported on buildkit yet.") - cmd.Flags().MarkHidden("memory") //nolint:errcheck + cmd.Flags().VarP(&opts.memory, "memory", "m", "Set memory limit for the build container. Not supported by BuildKit.") return cmd } @@ -141,5 +138,7 @@ func runBuild(ctx context.Context, backend api.Service, opts buildOptions, servi if err != nil { return err } + + apiBuildOptions.Memory = int64(opts.memory) return backend.Build(ctx, project, apiBuildOptions) } diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 019213d8a4..397cccda68 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -374,7 +374,7 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no portCommand(&opts, streams, backend), imagesCommand(&opts, streams, backend), versionCommand(streams), - buildCommand(&opts, streams, backend), + buildCommand(&opts, backend), pushCommand(&opts, backend), pullCommand(&opts, backend), createCommand(&opts, backend), diff --git a/docs/reference/compose_build.md b/docs/reference/compose_build.md index e631c25dac..a26dfeb6c2 100644 --- a/docs/reference/compose_build.md +++ b/docs/reference/compose_build.md @@ -5,15 +5,16 @@ Build or rebuild services ### Options -| Name | Type | Default | Description | -|:----------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------| -| `--build-arg` | `stringArray` | | Set build-time variables for services. | -| `--no-cache` | | | Do not use cache when building the image | -| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) | -| `--pull` | | | Always attempt to pull a newer version of the image. | -| `--push` | | | Push service images. | -| `-q`, `--quiet` | | | Don't print anything to STDOUT | -| `--ssh` | `string` | | Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent) | +| Name | Type | Default | Description | +|:-----------------|:--------------|:--------|:------------------------------------------------------------------------------------------------------------| +| `--build-arg` | `stringArray` | | Set build-time variables for services. | +| `-m`, `--memory` | `bytes` | `0` | Set memory limit for the build container. Not supported by BuildKit. | +| `--no-cache` | | | Do not use cache when building the image | +| `--progress` | `string` | `auto` | Set type of progress output (auto, tty, plain, quiet) | +| `--pull` | | | Always attempt to pull a newer version of the image. | +| `--push` | | | Push service images. | +| `-q`, `--quiet` | | | Don't print anything to STDOUT | +| `--ssh` | `string` | | Set SSH authentications used when building service images. (use 'default' for using your default SSH Agent) | diff --git a/docs/reference/docker_compose_build.yaml b/docs/reference/docker_compose_build.yaml index 11049c99e3..30de722f24 100644 --- a/docs/reference/docker_compose_build.yaml +++ b/docs/reference/docker_compose_build.yaml @@ -46,11 +46,12 @@ options: swarm: false - option: memory shorthand: m - value_type: string + value_type: bytes + default_value: "0" description: | - Set memory limit for the build container. Not supported on buildkit yet. + Set memory limit for the build container. Not supported by BuildKit. deprecated: false - hidden: true + hidden: false experimental: false experimentalcli: false kubernetes: false diff --git a/pkg/api/api.go b/pkg/api/api.go index 868bf11659..a31e32e992 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -119,6 +119,8 @@ type BuildOptions struct { Services []string // Ssh authentications passed in the command line SSHs []types.SSHKey + // Memory limit for the build container + Memory int64 } // Apply mutates project according to build options diff --git a/pkg/compose/build.go b/pkg/compose/build.go index be176bba7a..900eb7971e 100644 --- a/pkg/compose/build.go +++ b/pkg/compose/build.go @@ -91,7 +91,7 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti } else { service.Build.Args = service.Build.Args.OverrideBy(args) } - id, err := s.doBuildClassic(ctx, service) + id, err := s.doBuildClassic(ctx, service, options) if err != nil { return err } @@ -102,6 +102,11 @@ func (s *composeService) build(ctx context.Context, project *types.Project, opti } return nil } + + if options.Memory != 0 { + fmt.Fprintln(s.stderr(), "WARNING: --memory is not supported by BuildKit and will be ignored.") + } + buildOptions, err := s.toBuildOptions(project, service, options) if err != nil { return err diff --git a/pkg/compose/build_classic.go b/pkg/compose/build_classic.go index ceeef9b475..fb7697446d 100644 --- a/pkg/compose/build_classic.go +++ b/pkg/compose/build_classic.go @@ -43,7 +43,7 @@ import ( ) //nolint:gocyclo -func (s *composeService) doBuildClassic(ctx context.Context, service types.ServiceConfig) (string, error) { +func (s *composeService) doBuildClassic(ctx context.Context, service types.ServiceConfig, options api.BuildOptions) (string, error) { var ( buildCtx io.ReadCloser dockerfileCtx io.ReadCloser @@ -161,6 +161,7 @@ func (s *composeService) doBuildClassic(ctx context.Context, service types.Servi buildOptions.Tags = append(buildOptions.Tags, service.Image) buildOptions.Dockerfile = relDockerfile buildOptions.AuthConfigs = authConfigs + buildOptions.Memory = options.Memory ctx, cancel := context.WithCancel(ctx) defer cancel()