From 6ceb0aba82685882cbf68034defbd8e242d45cfa Mon Sep 17 00:00:00 2001 From: Albin Kerouanton Date: Tue, 20 Aug 2024 17:31:07 +0200 Subject: [PATCH] cli/formatter: bracket IPv6 addrs prepended to ports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On `docker ps`, port bindings with an IPv6 HostIP should have their addresses put into brackets when joining them to their ports. RFC 3986 (Section 3.2.2) stipulates that IPv6 addresses should be enclosed within square brackets. This RFC is only about URIs. However, doing so here helps user identifier what's part of the IP address and what's the port. It also makes it easier to copy/paste that '[addr]:port' into other software (including browsers). Signed-off-by: Albin Kerouanton (cherry picked from commit 964155cd27dd17eff499d1808b4d2e103c1ea170) Signed-off-by: Paweł Gronowski --- cli/command/formatter/container.go | 4 +++- cli/command/formatter/container_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cli/command/formatter/container.go b/cli/command/formatter/container.go index aeaa03a6d3b1..63c1a73b19da 100644 --- a/cli/command/formatter/container.go +++ b/cli/command/formatter/container.go @@ -5,6 +5,7 @@ package formatter import ( "fmt" + "net" "sort" "strconv" "strings" @@ -331,7 +332,8 @@ func DisplayablePorts(ports []types.Port) string { portKey := port.Type if port.IP != "" { if port.PublicPort != current { - hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type)) + hAddrPort := net.JoinHostPort(port.IP, strconv.Itoa(int(port.PublicPort))) + hostMappings = append(hostMappings, fmt.Sprintf("%s->%d/%s", hAddrPort, port.PrivatePort, port.Type)) continue } portKey = port.IP + "/" + port.Type diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index 8f0d5e5c31d6..9ef64ec895ec 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -471,6 +471,16 @@ func TestDisplayablePorts(t *testing.T) { }, "0.0.0.0:0->9988/tcp", }, + { + []types.Port{ + { + IP: "::", + PrivatePort: 9988, + Type: "tcp", + }, + }, + "[::]:0->9988/tcp", + }, { []types.Port{ {