From d47f0f31c2fd4162e9a0ed683896431d62aa66fc Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Tue, 24 Jan 2023 15:53:46 +0100 Subject: [PATCH 1/2] Update docs to mention `COMPOSE_PARALLEL_LIMITS` and ways to configure parallelism Signed-off-by: Laura Brehm --- docs/reference/compose.md | 11 ++++++++++- docs/reference/docker_compose.yaml | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/reference/compose.md b/docs/reference/compose.md index 8ca43277d42..8bda2056f30 100644 --- a/docs/reference/compose.md +++ b/docs/reference/compose.md @@ -141,13 +141,22 @@ You can also enable multiple profiles, e.g. with `docker compose --profile front Profiles can also be set by `COMPOSE_PROFILES` environment variable. +### Configuring parallelism + +Use `--parallel` to specify the maximum level of parallelism for concurrent engine calls. +Calling `docker compose --parallel 1 pull` will pull the pullable images defined in the Compose file +one at a time. This can also be used to control build concurrency. + +Parallelism can also be set by the `COMPOSE_PARALLEL_LIMIT` environment variable. + ### Set up environment variables You can set environment variables for various docker compose options, including the `-f`, `-p` and `--profiles` flags. Setting the `COMPOSE_FILE` environment variable is equivalent to passing the `-f` flag, `COMPOSE_PROJECT_NAME` environment variable does the same as the `-p` flag, -and `COMPOSE_PROFILES` environment variable is equivalent to the `--profiles` flag. +`COMPOSE_PROFILES` environment variable is equivalent to the `--profiles` flag +and `COMPOSE_PARALLEL_LIMIT` does the same as the `--parallel` flag. If flags are explicitly set on the command line, the associated environment variable is ignored. diff --git a/docs/reference/docker_compose.yaml b/docs/reference/docker_compose.yaml index 190cf99fdba..f4ec4badf87 100644 --- a/docs/reference/docker_compose.yaml +++ b/docs/reference/docker_compose.yaml @@ -89,13 +89,22 @@ long: |- Profiles can also be set by `COMPOSE_PROFILES` environment variable. + ### Configuring parallelism + + Use `--parallel` to specify the maximum level of parallelism for concurrent engine calls. + Calling `docker compose --parallel 1 pull` will pull the pullable images defined in the Compose file + one at a time. This can also be used to control build concurrency. + + Parallelism can also be set by the `COMPOSE_PARALLEL_LIMIT` environment variable. + ### Set up environment variables You can set environment variables for various docker compose options, including the `-f`, `-p` and `--profiles` flags. Setting the `COMPOSE_FILE` environment variable is equivalent to passing the `-f` flag, `COMPOSE_PROJECT_NAME` environment variable does the same as the `-p` flag, - and `COMPOSE_PROFILES` environment variable is equivalent to the `--profiles` flag. + `COMPOSE_PROFILES` environment variable is equivalent to the `--profiles` flag + and `COMPOSE_PARALLEL_LIMIT` does the same as the `--parallel` flag. If flags are explicitly set on the command line, the associated environment variable is ignored. From 8610cbd8fe2b0f51000ee4200ac52a16fbdb18e1 Mon Sep 17 00:00:00 2001 From: Vedant Koditkar Date: Tue, 24 Jan 2023 23:08:41 +0530 Subject: [PATCH 2/2] Add service-env-file option to pass service env file --- cmd/compose/run.go | 48 +++++++++++++++++----------------- pkg/api/api.go | 4 +-- pkg/compose/run.go | 4 +-- pkg/compose/run_test.go | 58 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 pkg/compose/run_test.go diff --git a/cmd/compose/run.go b/cmd/compose/run.go index fd18c94fb02..ff2657f79b5 100644 --- a/cmd/compose/run.go +++ b/cmd/compose/run.go @@ -36,28 +36,28 @@ import ( type runOptions struct { *composeOptions - Service string - Command []string - environment []string - Detach bool - Remove bool - noTty bool - tty bool - interactive bool - user string - workdir string - entrypoint string - entrypointCmd []string - labels []string - volumes []string - publish []string - useAliases bool - servicePorts bool - name string - noDeps bool - ignoreOrphans bool - quietPull bool - EnvFiles []string + Service string + Command []string + environment []string + Detach bool + Remove bool + noTty bool + tty bool + interactive bool + user string + workdir string + entrypoint string + entrypointCmd []string + labels []string + volumes []string + publish []string + useAliases bool + servicePorts bool + name string + noDeps bool + ignoreOrphans bool + quietPull bool + ServiceEnvFiles []string } func (opts runOptions) apply(project *types.Project) error { @@ -157,7 +157,7 @@ func runCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *co flags := cmd.Flags() flags.BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID") flags.StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables") - flags.StringArrayVar(&opts.EnvFiles, "env-file", []string{}, "Read in a file of environment variables") + flags.StringArrayVar(&opts.ServiceEnvFiles, "service-env-file", []string{}, "Read in a file of environment variables") flags.StringArrayVarP(&opts.labels, "label", "l", []string{}, "Add or override a label") flags.BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits") flags.BoolVarP(&opts.noTty, "no-TTY", "T", !streams.Out().IsTerminal(), "Disable pseudo-TTY allocation (default: auto-detected).") @@ -235,7 +235,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op NoDeps: opts.noDeps, Index: 0, QuietPull: opts.quietPull, - EnvFiles: opts.EnvFiles, + ServiceEnvFiles: opts.ServiceEnvFiles, } for i, service := range project.Services { diff --git a/pkg/api/api.go b/pkg/api/api.go index fcc36aab424..5820b8ca756 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -259,8 +259,8 @@ type RunOptions struct { // QuietPull makes the pulling process quiet QuietPull bool // used by exec - Index int - EnvFiles []string + Index int + ServiceEnvFiles []string } // EventsOptions group options of the Events API diff --git a/pkg/compose/run.go b/pkg/compose/run.go index 7bb96e269f9..28ecb5490ab 100644 --- a/pkg/compose/run.go +++ b/pkg/compose/run.go @@ -128,7 +128,7 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts service.Labels = service.Labels.Add(k, v) } - if opts.EnvFiles != nil { - service.EnvFile = opts.EnvFiles + if opts.ServiceEnvFiles != nil { + service.EnvFile = append(service.EnvFile, opts.ServiceEnvFiles...) } } diff --git a/pkg/compose/run_test.go b/pkg/compose/run_test.go new file mode 100644 index 00000000000..4cb6f785ee7 --- /dev/null +++ b/pkg/compose/run_test.go @@ -0,0 +1,58 @@ +package compose + +import ( + "testing" + + "github.com/compose-spec/compose-go/types" + "github.com/docker/compose/v2/pkg/api" + "gotest.tools/v3/assert" +) + +func TestServiceEnvFiles(t *testing.T) { + + t.Run("Verify service.EnvFile shouldn't modify", func(t *testing.T) { + fooService := types.ServiceConfig{ + Name: "foo", + EnvFile: []string{}, + } + + project := types.Project{ + Name: "test-project", + Services: types.Services{ + fooService, + }, + } + + opts := api.RunOptions{ + ServiceEnvFiles: nil, + } + + applyRunOptions(&project, &fooService, opts) + + assert.Assert(t, len(fooService.EnvFile) == 0) + }) + + t.Run("Verify appends ServiceEnvFiles", func(t *testing.T) { + fooService := &types.ServiceConfig{ + Name: "foo", + EnvFile: []string{"./existing.env"}, + } + + project := types.Project{ + Name: "test-project", + Services: types.Services{ + *fooService, + }, + } + + opts := api.RunOptions{ + ServiceEnvFiles: []string{"./file.env"}, + } + + applyRunOptions(&project, fooService, opts) + + assert.Assert(t, len(fooService.EnvFile) == 2) + assert.Assert(t, fooService.EnvFile[0] == "./existing.env") + assert.Assert(t, fooService.EnvFile[1] == "./file.env") + }) +}