From f1048e1a3a84733b4ec7217088394103be28345f Mon Sep 17 00:00:00 2001 From: Albin Kerouanton Date: Thu, 10 Aug 2023 12:52:31 +0200 Subject: [PATCH] Create default EndpointSettings if no --network provided Following flags are silently ignored when they're passed with no `--network` specified (ie. when the default network is used): - `--network-alias` - `--ip` - `--ip6` - `--link-local-ip` This is not really an issue right now since the first 3 parameters are not allowed on the default bridge network. However, with [moby/moby#45905][1], the container-wide MacAddress parameter will be deprecated and dismissed. Because of that, with [docker/cli#4419][2], it's currently not possible to use the `--mac-address` flag with no default network specified. Morever, `docker network connect --link-local-ip ...` works properly, so it should also work on `docker container create`. This also lay the ground for making the default bridge network just a "normal" network. Since the 3 parameters in the list above aren't ignored anymore, if users provide them, moby's ContainerStart endpoint will complain about those. To provide better UX, [moby/moby#46183][3] make sure these invalid parameters lead to a proper error message on `docker container create` / `docker run`. [1]: https://github.com/moby/moby/pull/45905 [2]: https://github.com/docker/cli/pull/4419 [3]: https://github.com/moby/moby/pull/46183 Signed-off-by: Albin Kerouanton --- cli/command/container/opts.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index cd5d69f7e791..5504e0611b49 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -732,6 +732,20 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*networktypes.Endpoin hasUserDefined, hasNonUserDefined bool ) + if len(copts.netMode.Value()) == 0 { + n := opts.NetworkAttachmentOpts{ + Target: "default", + } + if err := applyContainerOptions(&n, copts); err != nil { + return nil, err + } + ep, err := parseNetworkAttachmentOpt(n) + if err != nil { + return nil, err + } + endpoints["default"] = ep + } + for i, n := range copts.netMode.Value() { n := n if container.NetworkMode(n.Target).IsUserDefined() { @@ -792,7 +806,7 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption n.Aliases = make([]string, copts.aliases.Len()) copy(n.Aliases, copts.aliases.GetAll()) } - if copts.links.Len() > 0 { + if n.Target != "default" && copts.links.Len() > 0 { n.Links = make([]string, copts.links.Len()) copy(n.Links, copts.links.GetAll()) }