{"record":{"id":"239d347e9b45bddc","repo":"docker/cli","slug":"conflicting-options-cannot-attach-both-user-defin","errorCode":null,"errorMessage":"conflicting options: cannot attach both user-defined and non-user-defined network-modes","messagePattern":"conflicting options: cannot attach both user-defined and non-user-defined network-modes","errorType":"validation","errorClass":"invalidParameterErr","httpStatus":null,"severity":"error","filePath":"cli/command/container/opts.go","lineNumber":805,"sourceCode":"\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif _, ok := endpoints[n.Target]; ok {\n\t\t\treturn nil, invalidParameter(fmt.Errorf(\"network %q is specified multiple times\", n.Target))\n\t\t}\n\n\t\t// For backward compatibility: if no custom options are provided for the network,\n\t\t// and only a single network is specified, omit the endpoint-configuration\n\t\t// on the client (the daemon will still create it when creating the container)\n\t\tif i == 0 && len(copts.netMode.Value()) == 1 {\n\t\t\tif ep == nil || reflect.ValueOf(*ep).IsZero() {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\tendpoints[n.Target] = ep\n\t}\n\tif hasUserDefined && hasNonUserDefined {\n\t\treturn nil, invalidParameter(errors.New(\"conflicting options: cannot attach both user-defined and non-user-defined network-modes\"))\n\t}\n\treturn endpoints, nil\n}\n\nfunc applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOptions) error { //nolint:gocyclo\n\t// TODO should we error if _any_ advanced option is used? (i.e. forbid to combine advanced notation with the \"old\" flags (`--network-alias`, `--link`, `--ip`, `--ip6`)?\n\tif len(n.Aliases) > 0 && copts.aliases.Len() > 0 {\n\t\treturn invalidParameter(errors.New(\"conflicting options: cannot specify both --network-alias and per-network alias\"))\n\t}\n\tif len(n.Links) > 0 && copts.links.Len() > 0 {\n\t\treturn invalidParameter(errors.New(\"conflicting options: cannot specify both --link and per-network links\"))\n\t}\n\tif n.IPv4Address.IsValid() && copts.ipv4Address != nil {\n\t\treturn invalidParameter(errors.New(\"conflicting options: cannot specify both --ip and per-network IPv4 address\"))\n\t}\n\tif n.IPv6Address.IsValid() && copts.ipv6Address != nil {\n\t\treturn invalidParameter(errors.New(\"conflicting options: cannot specify both --ip6 and per-network IPv6 address\"))\n\t}","sourceCodeStart":787,"sourceCodeEnd":823,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/container/opts.go#L787-L823","documentation":"Returned by parseNetworkOpts when the set of --network targets mixes at least one user-defined network (a named network like \"my-net\") with at least one non-user-defined mode (\"bridge\", \"host\", \"none\", \"container:...\") (opts.go:770-806). hasUserDefined and hasNonUserDefined both becoming true triggers invalidParameter. The check iterates copts.netMode.Value() and tests each Target with container.NetworkMode.IsUserDefined().","triggerScenarios":"Running `docker run --network my-net --network host ...` or `--network bridge --network my-net ...` or `--network my-net --network none ...`. Any combination where one Target is a custom network name and another is a builtin mode.","commonSituations":"Migrating from a single --network to multi-network attach and leaving a leftover --network=bridge; compose-style configs flattening into multiple --network flags; assuming builtin modes can be combined with user-defined networks in one run.","solutions":["Use only user-defined networks when attaching to multiple: --network my-net --network my-net2.","Drop the builtin mode (--network host/bridge/none) from the multi-network command.","If you need host networking, attach only to host (single network)."],"exampleFix":"// before\ndocker run --network app-net --network host myimage\n// after\ndocker run --network app-net myimage","handlingStrategy":"validation","validationCode":"// Ensure all --network targets are the same class before invoking.\nvar hasUD, hasNonUD bool\nfor _, n := range copts.netMode.Value() {\n    if container.NetworkMode(n.Target).IsUserDefined() {\n        hasUD = true\n    } else {\n        hasNonUD = true\n    }\n}\nif hasUD && hasNonUD {\n    return errors.New(\"cannot mix user-defined and builtin network modes\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When attaching multiple networks, use only user-defined names.","Drop builtin modes (host/bridge/none/container:) from multi-network commands.","Validate network lists in orchestration templates."],"tags":["docker-cli","network","conflicting-options","validation","container-create"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}