diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index e95c855393..19a2d158cc 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -6,6 +6,7 @@ import ( "fmt" "io/ioutil" "path" + "reflect" "regexp" "strconv" "strings" @@ -707,6 +708,15 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*networktypes.Endpoin if _, ok := endpoints[n.Target]; ok { return nil, errdefs.InvalidParameter(errors.Errorf("network %q is specified multiple times", n.Target)) } + + // For backward compatibility: if no custom options are provided for the network, + // and only a single network is specified, omit the endpoint-configuration + // on the client (the daemon will still create it when creating the container) + if i == 0 && len(copts.netMode.Value()) == 1 { + if ep == nil || reflect.DeepEqual(*ep, networktypes.EndpointSettings{}) { + continue + } + } endpoints[n.Target] = ep } if hasUserDefined && hasNonUserDefined { diff --git a/cli/command/container/opts_test.go b/cli/command/container/opts_test.go index 6f0ad528fe..a64b7c6a30 100644 --- a/cli/command/container/opts_test.go +++ b/cli/command/container/opts_test.go @@ -401,13 +401,13 @@ func TestParseNetworkConfig(t *testing.T) { { name: "single-network-legacy", flags: []string{"--network", "net1"}, - expected: map[string]*networktypes.EndpointSettings{"net1": {}}, + expected: map[string]*networktypes.EndpointSettings{}, expectedCfg: container.HostConfig{NetworkMode: "net1"}, }, { name: "single-network-advanced", flags: []string{"--network", "name=net1"}, - expected: map[string]*networktypes.EndpointSettings{"net1": {}}, + expected: map[string]*networktypes.EndpointSettings{}, expectedCfg: container.HostConfig{NetworkMode: "net1"}, }, {