diff options
author | Valentin Rothberg <rothberg@redhat.com> | 2021-12-21 10:50:32 +0100 |
---|---|---|
committer | Valentin Rothberg <rothberg@redhat.com> | 2021-12-22 10:15:24 +0100 |
commit | 04dbbd96b6f69f65ac809938f44888728119db87 (patch) | |
tree | ca2595f4c65c030417a094602dff421aa3fb421c /libpod | |
parent | d2fcfef8b07532dc7418079160f0d6b989143c25 (diff) | |
download | podman-04dbbd96b6f69f65ac809938f44888728119db87.tar.gz podman-04dbbd96b6f69f65ac809938f44888728119db87.tar.bz2 podman-04dbbd96b6f69f65ac809938f44888728119db87.zip |
support hosts without /etc/hosts
Fixes: #12667
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/container_internal_linux.go | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go index 9e6ae9f02..7fd392fdc 100644 --- a/libpod/container_internal_linux.go +++ b/libpod/container_internal_linux.go @@ -1735,11 +1735,9 @@ func (c *Container) makeBindMounts() error { } if !c.config.UseImageHosts { - newHosts, err := c.generateHosts("/etc/hosts") - if err != nil { + if err := c.updateHosts("/etc/hosts"); err != nil { return errors.Wrapf(err, "error creating hosts file for container %s", c.ID()) } - c.state.BindMounts["/etc/hosts"] = newHosts } } @@ -1756,11 +1754,9 @@ func (c *Container) makeBindMounts() error { } } else { if !c.config.UseImageHosts && c.state.BindMounts["/etc/hosts"] == "" { - newHosts, err := c.generateHosts("/etc/hosts") - if err != nil { + if err := c.updateHosts("/etc/hosts"); err != nil { return errors.Wrapf(err, "error creating hosts file for container %s", c.ID()) } - c.state.BindMounts["/etc/hosts"] = newHosts } } @@ -2048,18 +2044,29 @@ func (c *Container) generateResolvConf() (string, error) { return destPath, nil } -// generateHosts creates a containers hosts file -func (c *Container) generateHosts(path string) (string, error) { +// updateHosts updates the container's hosts file +func (c *Container) updateHosts(path string) error { + var hosts string + orig, err := ioutil.ReadFile(path) if err != nil { - return "", err + // Ignore if the path does not exist + if !os.IsNotExist(err) { + return err + } + } else { + hosts = string(orig) } - hosts := string(orig) - hosts += c.getHosts() + hosts += c.getHosts() hosts = c.appendLocalhost(hosts) - return c.writeStringToRundir("hosts", hosts) + newHosts, err := c.writeStringToRundir("hosts", hosts) + if err != nil { + return err + } + c.state.BindMounts["/etc/hosts"] = newHosts + return nil } // based on networking mode we may want to append the localhost |