{"record":{"id":"2199908fc49af0b2","repo":"docker/cli","slug":"conflicting-options-cannot-specify-both-host-an","errorCode":null,"errorMessage":"conflicting options: cannot specify both --host and --context","messagePattern":"conflicting options: cannot specify both --host and --context","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/cli.go","lineNumber":227,"sourceCode":"// Initialize the dockerCli runs initialization that must happen after command\n// line flags are parsed.\nfunc (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption) error {\n\tfor _, o := range ops {\n\t\tif err := o(cli); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tcliflags.SetLogLevel(opts.LogLevel)\n\n\tif opts.ConfigDir != \"\" {\n\t\tconfig.SetDir(opts.ConfigDir)\n\t}\n\n\tif opts.Debug {\n\t\tdebug.Enable()\n\t}\n\tif opts.Context != \"\" && len(opts.Hosts) > 0 {\n\t\treturn errors.New(\"conflicting options: cannot specify both --host and --context\")\n\t}\n\n\tif cli.contextStoreConfig == nil {\n\t\t// This path can be hit when calling Initialize on a DockerCli that's\n\t\t// not constructed through [NewDockerCli]. Using the default context\n\t\t// store without a config set will result in Endpoints from contexts\n\t\t// not being type-mapped correctly, and used as a generic \"map[string]any\",\n\t\t// instead of a [docker.EndpointMeta].\n\t\t//\n\t\t// When looking up the API endpoint (using [EndpointFromContext]), no\n\t\t// endpoint will be found, and a default, empty endpoint will be used\n\t\t// instead which in its turn, causes newAPIClientFromEndpoint to\n\t\t// be initialized with the default config instead of settings for\n\t\t// the current context (which may mean; connecting with the wrong\n\t\t// endpoint and/or TLS Config to be missing).\n\t\t//\n\t\t// [EndpointFromContext]: https://github.com/docker/cli/blob/33494921b80fd0b5a06acc3a34fa288de4bb2e6b/cli/context/docker/load.go#L139-L149\n\t\tif err := WithDefaultContextStoreConfig()(cli); err != nil {","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/cli.go#L209-L245","documentation":"DockerCli.Initialize (cli/command/cli.go:211) validates that the --context and --host flags are not both supplied at line 226-227: if opts.Context is non-empty AND len(opts.Hosts) > 0, it returns errors.New(\"conflicting options: cannot specify both --host and --context\"). These options specify the daemon endpoint in two mutually exclusive ways (a named context vs. an explicit host URL).","triggerScenarios":"Running `docker --context myctx --host tcp://1.2.3.4:2375 ...`, or setting both DOCKER_HOST env var and --context flag, or any combination where a non-empty --context coexists with one or more --host/-H values.","commonSituations":"Aliases/wrappers that always pass --host colliding with a user-specified --context; shell history editing; migrating from DOCKER_HOST to contexts without removing the old setting; scripts combining flags from different sources.","solutions":["Use ONLY --context (and `docker context use`) OR only --host/-H — never both.","Unset DOCKER_HOST (`unset DOCKER_HOST`) if you intend to use --context.","Check your shell alias/functions (`alias docker`) for a hardcoded --host.","Run `docker context ls` and `echo $DOCKER_HOST` to identify which is being supplied."],"exampleFix":"# before\ndocker --context remote --host tcp://1.2.3.4:2376 ps\n# after — pick one\ndocker --context remote ps\n# or\ndocker --host tcp://1.2.3.4:2376 ps","handlingStrategy":"validation","validationCode":"// Resolve a single endpoint source before constructing options:\nif ctxName != \"\" && host != \"\" {\n    return errors.New(\"refusing to set both --context and --host; pick one\")\n}\nopts.Context, opts.Hosts = ctxName, hosts","typeGuard":null,"tryCatchPattern":"if err := cli.Initialize(opts); err != nil {\n    if strings.Contains(err.Error(), \"conflicting options: cannot specify both --host and --context\") {\n        // guidance: clear one of the two and retry / report to user\n    }\n    return err\n}","preventionTips":["Standardize on Docker contexts; avoid mixing --host and --context.","Unset DOCKER_HOST when using contexts.","Audit shell aliases/functions for hardcoded -H.","Document the chosen endpoint mechanism for your team."],"tags":["cli","context","host","configuration","conflicting-flags"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}