Skip to content

Commit

Permalink
add host_containers_internal_ip to containers.conf
Browse files Browse the repository at this point in the history
Set the ip for the host.containers.internal entry in the containers /etc/hosts
file. This can be set to "none" to disable adding this entry. By default it
will automatically choose the host ip.

Also add a function to get the correct host.containers.internal ip. This
should be used by podman and buildah and then passed to the New()
function.

Ref containers/podman#13224

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Apr 19, 2022
1 parent 7886c9d commit 85bda3a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/containers.conf.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ environment variables to the container.

Pass all host environment variables into the container.

**host_containers_internal_ip**=""

Set the ip for the host.containers.internal entry in the containers /etc/hosts
file. This can be set to "none" to disable adding this entry. By default it
will automatically choose the host ip.

NOTE: When using podman machine this entry will never be added to the containers
hosts file instead the gvproxy dns resolver will resolve this hostname. Therefore
it is not possible to disable the entry in this case.

**http_proxy**=true

Default proxy environment variables will be passed into the container.
Expand Down
70 changes: 70 additions & 0 deletions libnetwork/etchosts/ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package etchosts

import (
"net"

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/libnetwork/util"
"github.com/containers/common/pkg/config"
)

// GetHostContainersInternalIP return the host.containers.internal ip
// if netStatus is not nil then networkInterface also must be non nil otherwise this function panics
func GetHostContainersInternalIP(conf *config.Config, netStatus map[string]types.StatusBlock, networkInterface types.ContainerNetwork) string {
switch conf.Containers.HostContainersInternalIP {
case "":
// if empty (default) we will automatically choose one below
// if machine we let the gvproxy dns server handle the dns name so do not add it
if conf.Engine.MachineEnabled {
return ""
}
case "none":
return ""
default:
return conf.Containers.HostContainersInternalIP
}
ip := ""
for net, status := range netStatus {
network, err := networkInterface.NetworkInspect(net)
// only add the host entry for bridge networks
// ip/macvlan gateway is normally not on the host
if err != nil || network.Driver != types.BridgeNetworkDriver {
continue
}
for _, netInt := range status.Interfaces {
for _, netAddress := range netInt.Subnets {
if netAddress.Gateway != nil {
if util.IsIPv4(netAddress.Gateway) {
return netAddress.Gateway.String()
}
// ipv6 address but keep looking since we prefer to use ipv4
ip = netAddress.Gateway.String()
}
}
}
}
if ip != "" {
return ip
}
return getLocalIP()
}

// getLocalIP returns the non loopback local IP of the host
func getLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
ip := ""
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.IsGlobalUnicast() {
if util.IsIPv4(ipnet.IP) {
return ipnet.IP.String()
}
// if ipv6 we keep looking for an ipv4 address
ip = ipnet.IP.String()
}
}
return ip
}
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ type ContainersConfig struct {
// EnvHost Pass all host environment variables into the container.
EnvHost bool `toml:"env_host,omitempty"`

// HostContainersInternalIP is used to set a specific host.containers.internal ip.
HostContainersInternalIP string `toml:"host_containers_internal_ip,omitempty"`

// HTTPProxy is the proxy environment variable list to apply to container process
HTTPProxy bool `toml:"http_proxy,omitempty"`

Expand Down
1 change: 1 addition & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ image_copy_tmp_dir="storage"`
gomega.Expect(config.Containers.ApparmorProfile).To(gomega.Equal("container-default"))
gomega.Expect(config.Containers.PidsLimit).To(gomega.BeEquivalentTo(2048))
gomega.Expect(config.Containers.BaseHostsFile).To(gomega.BeEquivalentTo("/etc/hosts2"))
gomega.Expect(config.Containers.HostContainersInternalIP).To(gomega.BeEquivalentTo("1.2.3.4"))
})

It("contents of passed-in file should override others", func() {
Expand Down
10 changes: 10 additions & 0 deletions pkg/config/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ default_sysctls = [
#
#env_host = false

#Set the ip for the host.containers.internal entry in the containers /etc/hosts
#file. This can be set to "none" to disable adding this entry. By default it
#will automatically choose the host ip.
#
#NOTE: When using podman machine this entry will never be added to the containers
#hosts file instead the gvproxy dns resolver will resolve this hostname. Therefore
#it is not possible to disable the entry in this case.
#
#host_containers_internal_ip = ""

# Default proxy environment variables passed into the container.
# The environment variables passed in include:
# http_proxy, https_proxy, ftp_proxy, no_proxy, and the upper case versions of
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/testdata/containers_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ env = [
# Run an init inside the container that forwards signals and reaps processes.
init = false

host_containers_internal_ip = "1.2.3.4"

# proxy environment variables are passed into the container
http_proxy = false

Expand Down

0 comments on commit 85bda3a

Please sign in to comment.