diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2021-11-20 09:00:22 -0500 |
---|---|---|
committer | Daniel J Walsh <dwalsh@redhat.com> | 2021-12-21 08:50:40 -0500 |
commit | 64ce6949f23b308a432e83ec46c99ba67777ee1e (patch) | |
tree | 1da10ace481348ace677fbf0d815bc1967d7f05e /libpod/container_internal_linux.go | |
parent | f3d6672c7d95e09b312ac8db43e54618d4dd1662 (diff) | |
download | podman-64ce6949f23b308a432e83ec46c99ba67777ee1e.tar.gz podman-64ce6949f23b308a432e83ec46c99ba67777ee1e.tar.bz2 podman-64ce6949f23b308a432e83ec46c99ba67777ee1e.zip |
Use hosts public ip address in rootless containers
Add first non localhost ipv4 of all host interfaces as destination
for host.contaners.internal for rootless containers.
Fixes: https://github.com/containers/podman/issues/12000
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
Diffstat (limited to 'libpod/container_internal_linux.go')
-rw-r--r-- | libpod/container_internal_linux.go | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go index 9e6ae9f02..c2ec02e57 100644 --- a/libpod/container_internal_linux.go +++ b/libpod/container_internal_linux.go @@ -2154,11 +2154,24 @@ func (c *Container) getHosts() string { } } } else if c.config.NetMode.IsSlirp4netns() { - gatewayIP, err := GetSlirp4netnsGateway(c.slirp4netnsSubnet) - if err != nil { - logrus.Warn("Failed to determine gatewayIP: ", err.Error()) - } else { - hosts += fmt.Sprintf("%s host.containers.internal\n", gatewayIP.String()) + // getLocalIP returns the non loopback local IP of the host + getLocalIP := func() string { + addrs, err := net.InterfaceAddrs() + if err != nil { + return "" + } + for _, address := range addrs { + // check the address type and if it is not a loopback the display it + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP.String() + } + } + } + return "" + } + if ip := getLocalIP(); ip != "" { + hosts += fmt.Sprintf("%s\t%s\n", ip, "host.containers.internal") } } else { logrus.Debug("Network configuration does not support host.containers.internal address") |