Skip to content

Commit 6ceb0ab

Browse files
akerouantonvvoland
authored andcommitted
cli/formatter: bracket IPv6 addrs prepended to ports
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 <[email protected]> (cherry picked from commit 964155c) Signed-off-by: Paweł Gronowski <[email protected]>
1 parent cabd410 commit 6ceb0ab

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cli/command/formatter/container.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package formatter
55

66
import (
77
"fmt"
8+
"net"
89
"sort"
910
"strconv"
1011
"strings"
@@ -331,7 +332,8 @@ func DisplayablePorts(ports []types.Port) string {
331332
portKey := port.Type
332333
if port.IP != "" {
333334
if port.PublicPort != current {
334-
hostMappings = append(hostMappings, fmt.Sprintf("%s:%d->%d/%s", port.IP, port.PublicPort, port.PrivatePort, port.Type))
335+
hAddrPort := net.JoinHostPort(port.IP, strconv.Itoa(int(port.PublicPort)))
336+
hostMappings = append(hostMappings, fmt.Sprintf("%s->%d/%s", hAddrPort, port.PrivatePort, port.Type))
335337
continue
336338
}
337339
portKey = port.IP + "/" + port.Type

cli/command/formatter/container_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,16 @@ func TestDisplayablePorts(t *testing.T) {
471471
},
472472
"0.0.0.0:0->9988/tcp",
473473
},
474+
{
475+
[]types.Port{
476+
{
477+
IP: "::",
478+
PrivatePort: 9988,
479+
Type: "tcp",
480+
},
481+
},
482+
"[::]:0->9988/tcp",
483+
},
474484
{
475485
[]types.Port{
476486
{

0 commit comments

Comments
 (0)