restore support for --memory

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2023-04-24 10:51:40 +02:00 committed by Nicolas De loof
parent dec608f3cd
commit d01ef5887a
7 changed files with 31 additions and 22 deletions

View File

@ -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)
}

View File

@ -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),

View File

@ -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-->

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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()