docker/cli ยท error

conflicting options: cannot specify both --host and --contex

Error message

conflicting options: cannot specify both --host and --context

What it means

Raised in DockerCli.Initialize when both a docker context (--context or DOCKER_CONTEXT) and an explicit daemon host (--host/-H or DOCKER_HOST via flags) are supplied. Both mechanisms select which daemon endpoint to talk to, so specifying both is ambiguous; the CLI refuses to guess which should win and aborts before connecting.

Source

Thrown at cli/command/cli.go:227

// Initialize the dockerCli runs initialization that must happen after command
// line flags are parsed.
func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption) error {
	for _, o := range ops {
		if err := o(cli); err != nil {
			return err
		}
	}
	cliflags.SetLogLevel(opts.LogLevel)

	if opts.ConfigDir != "" {
		config.SetDir(opts.ConfigDir)
	}

	if opts.Debug {
		debug.Enable()
	}
	if opts.Context != "" && len(opts.Hosts) > 0 {
		return errors.New("conflicting options: cannot specify both --host and --context")
	}

	if cli.contextStoreConfig == nil {
		// This path can be hit when calling Initialize on a DockerCli that's
		// not constructed through [NewDockerCli]. Using the default context
		// store without a config set will result in Endpoints from contexts
		// not being type-mapped correctly, and used as a generic "map[string]any",
		// instead of a [docker.EndpointMeta].
		//
		// When looking up the API endpoint (using [EndpointFromContext]), no
		// endpoint will be found, and a default, empty endpoint will be used
		// instead which in its turn, causes newAPIClientFromEndpoint to
		// be initialized with the default config instead of settings for
		// the current context (which may mean; connecting with the wrong
		// endpoint and/or TLS Config to be missing).
		//
		// [EndpointFromContext]: https://github.com/docker/cli/blob/33494921b80fd0b5a06acc3a34fa288de4bb2e6b/cli/context/docker/load.go#L139-L149
		if err := WithDefaultContextStoreConfig()(cli); err != nil {

View on GitHub (pinned to e9452d6e78)

Solutions

  1. Drop the --host/-H flag and rely on the context: docker --context mycontext ...
  2. Or drop --context (and unset DOCKER_CONTEXT) and use --host alone
  3. Check for a lingering DOCKER_CONTEXT environment variable: unset DOCKER_CONTEXT
  4. Encode the desired host as a context instead: docker context create myhost --docker host=tcp://... then use --context myhost

Example fix

# before
DOCKER_CONTEXT=remote docker -H tcp://10.0.0.5:2376 ps
# after
docker --context remote ps

When it happens

Trigger: docker --context mycontext --host tcp://1.2.3.4:2376 ... ; or DOCKER_CONTEXT set in the environment combined with -H on the command line (opts.Context non-empty and len(opts.Hosts) > 0 at Initialize time).

Common situations: Shell profiles exporting DOCKER_CONTEXT while scripts pass -H; wrapper scripts/Makefiles adding -H to a CLI where the user has 'docker context use' set plus an explicit --context flag; CI pipelines mixing context-based and host-based daemon selection.

Related errors


AI-assisted analysis of docker/cli@e9452d6e78 (2026-08-01). Data as JSON: /data/errors/2199908fc49af0b2.json. Report an issue: GitHub.