diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index 94b8dffa34a6..0f187cf15271 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -170,7 +170,7 @@ func addFlags(flags *pflag.FlagSet) *containerOptions { groupAdd: opts.NewListOpts(nil), labels: opts.NewListOpts(opts.ValidateLabel), labelsFile: opts.NewListOpts(nil), - linkLocalIPs: opts.NewListOpts(nil), + linkLocalIPs: opts.NewListOpts(opts.ValidateIPAddress), links: opts.NewListOpts(opts.ValidateLink), loggingOpts: opts.NewListOpts(nil), publish: opts.NewListOpts(nil), @@ -843,14 +843,18 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption copy(n.Links, copts.links.GetSlice()) } if copts.ipv4Address != nil { - if ipv4, ok := netip.AddrFromSlice(copts.ipv4Address.To4()); ok { - n.IPv4Address = ipv4 + ipv4, ok := netip.AddrFromSlice(copts.ipv4Address.To4()) + if !ok { + return invalidParameter(fmt.Errorf("invalid IPv4 address for --ip: %s", copts.ipv4Address)) } + n.IPv4Address = ipv4 } if copts.ipv6Address != nil { - if ipv6, ok := netip.AddrFromSlice(copts.ipv6Address.To16()); ok { - n.IPv6Address = ipv6 + ipv6, ok := netip.AddrFromSlice(copts.ipv6Address.To16()) + if !ok || copts.ipv6Address.To4() != nil { + return invalidParameter(fmt.Errorf("invalid IPv6 address for --ip6: %s", copts.ipv6Address)) } + n.IPv6Address = ipv6 } if copts.macAddress != "" { n.MacAddress = copts.macAddress diff --git a/cli/command/container/opts_test.go b/cli/command/container/opts_test.go index 556e013066fa..cf529d5e862f 100644 --- a/cli/command/container/opts_test.go +++ b/cli/command/container/opts_test.go @@ -756,6 +756,21 @@ func TestParseNetworkConfig(t *testing.T) { flags: []string{"--network", "name=net1,mac-address=foobar"}, expectedErr: "foobar is not a valid mac address", }, + { + name: "invalid-ip-is-ipv6", + flags: []string{"--network", "net1", "--ip", "2001:db8::8822"}, + expectedErr: "invalid IPv4 address for --ip: 2001:db8::8822", + }, + { + name: "invalid-ip6-is-ipv4", + flags: []string{"--network", "net1", "--ip6", "172.20.88.22"}, + expectedErr: "invalid IPv6 address for --ip6: 172.20.88.22", + }, + { + name: "invalid-link-local-ip", + flags: []string{"--network", "net1", "--link-local-ip", "foobar"}, + expectedErr: `invalid argument "foobar" for "--link-local-ip" flag: IP address is not correctly formatted: foobar`, + }, } for _, tc := range tests {