{"id":"2a51253a20968b07","repo":"docker/cli","slug":"bad-format-for-add-host-q","errorCode":null,"errorMessage":"bad format for add-host: %q","messagePattern":"bad format for add-host: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"opts/hosts.go","lineNumber":174,"sourceCode":"//\tmy-hostname=::1\n//\tmy-hostname:[::1]\n//\n// For compatibility with the API server, this function normalises the given\n// argument to use the ':' separator and strip square brackets enclosing the\n// address.\nfunc ValidateExtraHost(val string) (string, error) {\n\tk, v, ok := strings.Cut(val, \"=\")\n\tif !ok {\n\t\t// allow for IPv6 addresses in extra hosts by only splitting on first \":\"\n\t\tk, v, ok = strings.Cut(val, \":\")\n\t}\n\t// Check that a hostname was given, and that it doesn't contain a \":\". (Colon\n\t// isn't allowed in a hostname, along with many other characters. It's\n\t// special-cased here because the API server doesn't know about '=' separators in\n\t// '--add-host'. So, it'll split at the first colon and generate a strange error\n\t// message.)\n\tif !ok || k == \"\" || strings.Contains(k, \":\") {\n\t\treturn \"\", fmt.Errorf(\"bad format for add-host: %q\", val)\n\t}\n\t// Skip IPaddr validation for \"host-gateway\" string\n\tif v != hostGatewayName {\n\t\t// If the address is enclosed in square brackets, extract it (for IPv6, but\n\t\t// permit it for IPv4 as well; we don't know the address family here, but it's\n\t\t// unambiguous).\n\t\tif len(v) > 2 && v[0] == '[' && v[len(v)-1] == ']' {\n\t\t\tv = v[1 : len(v)-1]\n\t\t}\n\t\t// ValidateIPAddress returns the address in canonical form (for example,\n\t\t// 0:0:0:0:0:0:0:1 -> ::1). But, stick with the original form, to avoid\n\t\t// surprising a user who's expecting to see the address they supplied in the\n\t\t// output of 'docker inspect' or '/etc/hosts'.\n\t\tif _, err := ValidateIPAddress(v); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"invalid IP address in add-host: %q\", v)\n\t\t}\n\t}\n\t// This result is passed directly to the API, the daemon doesn't accept the '='","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/docker/cli/blob/e9452d6e785f6e365712b9d71bd7517591773c86/opts/hosts.go#L156-L192","documentation":"Error \"bad format for add-host: %q\" thrown in docker/cli.","triggerScenarios":"Thrown at opts/hosts.go:174 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"analyzedSha":"e9452d6e785f6e365712b9d71bd7517591773c86","analyzedAt":"2026-08-01T07:09:22.474Z","schemaVersion":2}