From 5ee2906e784da604d0df340f9ba41a921a01f2f1 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 15 Aug 2025 23:39:39 +0200 Subject: [PATCH] cli/flags: use a regular StringArray for the `--host` / `-H` flag The ClientOptions struct and related flags were inherited from the Moby repository, where originally the CLI and Daemon used the same implementation and had a "Common" options struct. When the CLI moved to a separate repository, those structs were duplicated, but some daemon-specific logic remained. For example, the daemon can be configured to listen on multiple ports and sockets ([moby@dede158]), but the CLI [can only connect to a single host][1]. The daemon config also had to account for flags conflicting with `daemon.json`, and use special flag-vars for this ([moby@677a6b3]). Unfortunately, the `ClientConfig` struct became part of the public API and is used as argument in various places, but we can remove the use of the special flag var. This patch replaces the use of `NewNamedListOptsRef` for a regular `StringArray`. Unfortunately this changes the flag's type description from `list` to `stringArray`, but we can look at changing that separately. [moby@dede158]: https://github.com/moby/moby/commit/dede1585ee00f957e153691c464aab293c2dc469 [1]: https://github.com/moby/moby/blob/0af135e9065562e14a77439e13a29b4f1eb627a0/docker/docker.go#L191-L193 [moby@677a6b3]: https://github.com/moby/moby/commit/677a6b3506107468ed8c00331991afd9176fa0b9 Signed-off-by: Sebastiaan van Stijn --- cli/flags/options.go | 7 +++---- docs/reference/commandline/docker.md | 24 ++++++++++++------------ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/cli/flags/options.go b/cli/flags/options.go index aaa1600cbc..772a88ab28 100644 --- a/cli/flags/options.go +++ b/cli/flags/options.go @@ -6,7 +6,6 @@ import ( "path/filepath" "github.com/docker/cli/cli/config" - "github.com/docker/cli/opts" "github.com/docker/go-connections/tlsconfig" "github.com/moby/moby/client" "github.com/sirupsen/logrus" @@ -94,9 +93,9 @@ func (o *ClientOptions) InstallFlags(flags *pflag.FlagSet) { flags.Var("edString{&tlsOptions.CertFile}, "tlscert", "Path to TLS certificate file") flags.Var("edString{&tlsOptions.KeyFile}, "tlskey", "Path to TLS key file") - // opts.ValidateHost is not used here, so as to allow connection helpers - hostOpt := opts.NewNamedListOptsRef("hosts", &o.Hosts, nil) - flags.VarP(hostOpt, "host", "H", "Daemon socket to connect to") + // TODO(thaJeztah): show the default host. + // TODO(thaJeztah): this should be a string, not an "array" as we only allow a single host. + flags.StringArrayVarP(&o.Hosts, "host", "H", nil, "Daemon socket to connect to") flags.StringVarP(&o.Context, "context", "c", "", `Name of the context to use to connect to the daemon (overrides `+client.EnvOverrideHost+` env var and default context set with "docker context use")`) } diff --git a/docs/reference/commandline/docker.md b/docs/reference/commandline/docker.md index 69b1c91303..00ab5cce62 100644 --- a/docs/reference/commandline/docker.md +++ b/docs/reference/commandline/docker.md @@ -69,18 +69,18 @@ The base command for the Docker CLI. ### Options -| Name | Type | Default | Description | -|:---------------------------------|:---------|:-------------------------|:--------------------------------------------------------------------------------------------------------------------------------------| -| `--config` | `string` | `/root/.docker` | Location of client config files | -| `-c`, `--context` | `string` | | Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with `docker context use`) | -| `-D`, `--debug` | `bool` | | Enable debug mode | -| [`-H`](#host), [`--host`](#host) | `list` | | Daemon socket to connect to | -| `-l`, `--log-level` | `string` | `info` | Set the logging level (`debug`, `info`, `warn`, `error`, `fatal`) | -| `--tls` | `bool` | | Use TLS; implied by --tlsverify | -| `--tlscacert` | `string` | `/root/.docker/ca.pem` | Trust certs signed only by this CA | -| `--tlscert` | `string` | `/root/.docker/cert.pem` | Path to TLS certificate file | -| `--tlskey` | `string` | `/root/.docker/key.pem` | Path to TLS key file | -| `--tlsverify` | `bool` | | Use TLS and verify the remote | +| Name | Type | Default | Description | +|:---------------------------------|:--------------|:-------------------------|:--------------------------------------------------------------------------------------------------------------------------------------| +| `--config` | `string` | `/root/.docker` | Location of client config files | +| `-c`, `--context` | `string` | | Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with `docker context use`) | +| `-D`, `--debug` | `bool` | | Enable debug mode | +| [`-H`](#host), [`--host`](#host) | `stringArray` | | Daemon socket to connect to | +| `-l`, `--log-level` | `string` | `info` | Set the logging level (`debug`, `info`, `warn`, `error`, `fatal`) | +| `--tls` | `bool` | | Use TLS; implied by --tlsverify | +| `--tlscacert` | `string` | `/root/.docker/ca.pem` | Trust certs signed only by this CA | +| `--tlscert` | `string` | `/root/.docker/cert.pem` | Path to TLS certificate file | +| `--tlskey` | `string` | `/root/.docker/key.pem` | Path to TLS key file | +| `--tlsverify` | `bool` | | Use TLS and verify the remote |