Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions docs/containers.conf.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,10 @@ setup. Adding these internal hostnames to `/etc/hosts` is silently skipped then.
Set this config to `none` to never add the internal hostnames to `/etc/hosts`.

Note: If Podman is running in a virtual machine using `podman machine` (this
includes Mac and Windows hosts), Podman will silently skip adding the internal
hostnames to `/etc/hosts`, unless an IP address was configured manually. The
internal hostnames are resolved by the gvproxy DNS resolver instead. This config
has no effect on gvproxy. However, since `/etc/hosts` bypasses the DNS resolver,
a manually configured IP address still takes precedence.
includes Mac and Windows hosts), Podman resolves the `host.containers.internal`
hostname via the podman machine (gvproxy) DNS resolver instead when it is empty.
Also because the name will be resolved by the DNS name in gvproxy setting this
to `none` has no effect. This option does not change the gvproxy behavior.

Note: This config doesn't affect the actual network setup, it just tells Podman
the IP address it should expect. Configuring an IP address here doesn't ensure
Expand Down
24 changes: 21 additions & 3 deletions libnetwork/etchosts/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package etchosts

import (
"net"
"sync"

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/libnetwork/util"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/machine"
"github.com/containers/storage/pkg/unshare"
"github.com/sirupsen/logrus"
)

// HostContainersInternalOptions contains the options for GetHostContainersInternalIP()
Expand All @@ -28,14 +30,30 @@ type HostContainersInternalOptions struct {
PreferIP string
}

// Lookup "host.containers.internal" dns name so we can add it to /etc/hosts when running inside podman machine.
var machineHostContainersInternalIP = sync.OnceValue(func() string {
var errMsg string
addrs, err := net.LookupIP(HostContainersInternal)
if err == nil {
if len(addrs) > 0 {
return addrs[0].String()
}
errMsg = "lookup result is empty"
} else {
errMsg = err.Error()
}
logrus.Warnf("Failed to resolve %s for the host entry ip address: %s", HostContainersInternal, errMsg)
return ""
})

// GetHostContainersInternalIP returns the host.containers.internal ip
func GetHostContainersInternalIP(opts HostContainersInternalOptions) string {
switch opts.Conf.Containers.HostContainersInternalIP {
case "":
// if empty (default) we will automatically choose one below
// if machine using gvproxy we let the gvproxy dns server handle the dns name so do not add it
// If empty (default) we will automatically choose one below.
// If machine using gvproxy we let the gvproxy dns server handle resolve the name and then use that ip.
if machine.IsGvProxyBased() {
return ""
return machineHostContainersInternalIP()
}
case "none":
return ""
Expand Down