Skip to content

Commit

Permalink
restore support for --memory
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Apr 25, 2023
1 parent dec608f commit 64f1c8e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
13 changes: 6 additions & 7 deletions cmd/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,7 +43,7 @@ type buildOptions struct {
progress string
args []string
noCache bool
memory string
memory cliopts.MemBytes
ssh string
}

Expand Down Expand Up @@ -75,17 +76,14 @@ 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,
}
cmd := &cobra.Command{
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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
19 changes: 10 additions & 9 deletions docs/reference/compose_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |


<!---MARKER_GEN_END-->
Expand Down
7 changes: 4 additions & 3 deletions docs/reference/docker_compose_build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion pkg/compose/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/compose/build_classic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 64f1c8e

Please sign in to comment.