diff --git a/integration/container/nat_test.go b/integration/container/nat_test.go index a8d1134e4d..aad5175fd5 100644 --- a/integration/container/nat_test.go +++ b/integration/container/nat_test.go @@ -30,7 +30,7 @@ func TestNetworkNat(t *testing.T) { startServerContainer(t, msg, 8080) endpoint := getExternalAddress(t) - conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080)) + conn, err := net.Dial("tcp", net.JoinHostPort(endpoint.String(), "8080")) assert.NilError(t, err) defer conn.Close() @@ -115,6 +115,9 @@ func startServerContainer(t *testing.T, msg string, port int) string { return cID } +// getExternalAddress() returns the external IP-address from eth0. If eth0 has +// multiple IP-addresses, it returns the first IPv4 IP-address; if no IPv4 +// address is present, it returns the first IP-address found. func getExternalAddress(t *testing.T) net.IP { t.Helper() iface, err := net.InterfaceByName("eth0") @@ -124,6 +127,17 @@ func getExternalAddress(t *testing.T) net.IP { assert.NilError(t, err) assert.Check(t, 0 != len(ifaceAddrs)) + if len(ifaceAddrs) > 1 { + // Prefer IPv4 address if multiple addresses found, as rootlesskit + // does not handle IPv6 currently https://github.com/moby/moby/pull/41908#issuecomment-774200001 + for _, a := range ifaceAddrs { + ifaceIP, _, err := net.ParseCIDR(a.String()) + assert.NilError(t, err) + if ifaceIP.To4() != nil { + return ifaceIP + } + } + } ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String()) assert.NilError(t, err)