From a675a1e4d37559def9c28b2a8aced1ba4ac935b4 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Fri, 2 Aug 2024 00:21:44 +0000 Subject: [PATCH] run: add support --no-build, --pull flags Allows setting build- and pull policies for `compose run` like for `docker run` and `compose up`. Signed-off-by: legobt <6wbvkn0j@anonaddy.me> --- cmd/compose/run.go | 9 +++++ docs/reference/compose_run.md | 48 ++++++++++++++------------ docs/reference/docker_compose_run.yaml | 20 +++++++++++ 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/cmd/compose/run.go b/cmd/compose/run.go index b4a5da365fc..f0cb224d061 100644 --- a/cmd/compose/run.go +++ b/cmd/compose/run.go @@ -153,6 +153,13 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) * options.noTty = !options.tty } } + if cmd.Flags().Changed("build") { + if cmd.Flags().Changed("no-build") { + return fmt.Errorf("--build and --no-build are incompatible") + } else { + options.noTty = !options.tty + } + } return nil }), RunE: Adapt(func(ctx context.Context, args []string) error { @@ -189,6 +196,8 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) * flags.BoolVarP(&options.servicePorts, "service-ports", "P", false, "Run command with all service's ports enabled and mapped to the host") flags.BoolVar(&options.quietPull, "quiet-pull", false, "Pull without printing progress information") flags.BoolVar(&createOpts.Build, "build", false, "Build image before starting container") + flags.BoolVar(&createOpts.noBuild, "no-build", false, "Don't build an image, even if it's policy") + flags.StringVar(&createOpts.Pull, "pull", "policy", `Pull image before running ("always"|"missing"|"never")`) flags.BoolVar(&createOpts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file") cmd.Flags().BoolVarP(&options.interactive, "interactive", "i", true, "Keep STDIN open even if not attached") diff --git a/docs/reference/compose_run.md b/docs/reference/compose_run.md index 2d08ef4c4cb..df963d3a0c9 100644 --- a/docs/reference/compose_run.md +++ b/docs/reference/compose_run.md @@ -57,29 +57,31 @@ specified in the service configuration. ### Options -| Name | Type | Default | Description | -|:------------------------|:--------------|:--------|:---------------------------------------------------------------------------------| -| `--build` | `bool` | | Build image before starting container | -| `--cap-add` | `list` | | Add Linux capabilities | -| `--cap-drop` | `list` | | Drop Linux capabilities | -| `-d`, `--detach` | `bool` | | Run container in background and print container ID | -| `--dry-run` | `bool` | | Execute command in dry run mode | -| `--entrypoint` | `string` | | Override the entrypoint of the image | -| `-e`, `--env` | `stringArray` | | Set environment variables | -| `-i`, `--interactive` | `bool` | `true` | Keep STDIN open even if not attached | -| `-l`, `--label` | `stringArray` | | Add or override a label | -| `--name` | `string` | | Assign a name to the container | -| `-T`, `--no-TTY` | `bool` | `true` | Disable pseudo-TTY allocation (default: auto-detected) | -| `--no-deps` | `bool` | | Don't start linked services | -| `-p`, `--publish` | `stringArray` | | Publish a container's port(s) to the host | -| `--quiet-pull` | `bool` | | Pull without printing progress information | -| `--remove-orphans` | `bool` | | Remove containers for services not defined in the Compose file | -| `--rm` | `bool` | | Automatically remove the container when it exits | -| `-P`, `--service-ports` | `bool` | | Run command with all service's ports enabled and mapped to the host | -| `--use-aliases` | `bool` | | Use the service's network useAliases in the network(s) the container connects to | -| `-u`, `--user` | `string` | | Run as specified username or uid | -| `-v`, `--volume` | `stringArray` | | Bind mount a volume | -| `-w`, `--workdir` | `string` | | Working directory inside the container | +| Name | Type | Default | Description | +|:------------------------|:--------------|:---------|:---------------------------------------------------------------------------------| +| `--build` | `bool` | | Build image before starting container | +| `--cap-add` | `list` | | Add Linux capabilities | +| `--cap-drop` | `list` | | Drop Linux capabilities | +| `-d`, `--detach` | `bool` | | Run container in background and print container ID | +| `--dry-run` | `bool` | | Execute command in dry run mode | +| `--entrypoint` | `string` | | Override the entrypoint of the image | +| `-e`, `--env` | `stringArray` | | Set environment variables | +| `-i`, `--interactive` | `bool` | `true` | Keep STDIN open even if not attached | +| `-l`, `--label` | `stringArray` | | Add or override a label | +| `--name` | `string` | | Assign a name to the container | +| `-T`, `--no-TTY` | `bool` | `true` | Disable pseudo-TTY allocation (default: auto-detected) | +| `--no-build` | `bool` | | Don't build an image, even if it's policy | +| `--no-deps` | `bool` | | Don't start linked services | +| `-p`, `--publish` | `stringArray` | | Publish a container's port(s) to the host | +| `--pull` | `string` | `policy` | Pull image before running ("always"\|"missing"\|"never") | +| `--quiet-pull` | `bool` | | Pull without printing progress information | +| `--remove-orphans` | `bool` | | Remove containers for services not defined in the Compose file | +| `--rm` | `bool` | | Automatically remove the container when it exits | +| `-P`, `--service-ports` | `bool` | | Run command with all service's ports enabled and mapped to the host | +| `--use-aliases` | `bool` | | Use the service's network useAliases in the network(s) the container connects to | +| `-u`, `--user` | `string` | | Run as specified username or uid | +| `-v`, `--volume` | `stringArray` | | Bind mount a volume | +| `-w`, `--workdir` | `string` | | Working directory inside the container | diff --git a/docs/reference/docker_compose_run.yaml b/docs/reference/docker_compose_run.yaml index 0584bdd070b..fbc9930c705 100644 --- a/docs/reference/docker_compose_run.yaml +++ b/docs/reference/docker_compose_run.yaml @@ -159,6 +159,16 @@ options: experimentalcli: false kubernetes: false swarm: false + - option: no-build + value_type: bool + default_value: "false" + description: Don't build an image, even if it's policy + deprecated: false + hidden: false + experimental: false + experimentalcli: false + kubernetes: false + swarm: false - option: no-deps value_type: bool default_value: "false" @@ -180,6 +190,16 @@ options: experimentalcli: false kubernetes: false swarm: false + - option: pull + value_type: string + default_value: policy + description: Pull image before running ("always"|"missing"|"never") + deprecated: false + hidden: false + experimental: false + experimentalcli: false + kubernetes: false + swarm: false - option: quiet-pull value_type: bool default_value: "false"