summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-12-22 12:13:57 +0100
committerGitHub <noreply@github.com>2021-12-22 12:13:57 +0100
commit3280204f727bb733b1c615cff68e8377a61eb185 (patch)
treeee467b681487d07729e651662d37fbda7b72e91b
parent85f21fb2bd39d76300ab61a1fb1207405e05f9d2 (diff)
parent04dbbd96b6f69f65ac809938f44888728119db87 (diff)
downloadpodman-3280204f727bb733b1c615cff68e8377a61eb185.tar.gz
podman-3280204f727bb733b1c615cff68e8377a61eb185.tar.bz2
podman-3280204f727bb733b1c615cff68e8377a61eb185.zip
Merge pull request #12668 from vrothberg/fix-12667
support hosts without /etc/hosts
-rw-r--r--libpod/container_internal_linux.go31
-rw-r--r--test/system/030-run.bats10
2 files changed, 29 insertions, 12 deletions
diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go
index dcffc4292..9e83d6db5 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
}
}
@@ -2050,18 +2046,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
diff --git a/test/system/030-run.bats b/test/system/030-run.bats
index 6f1fa600a..55514305b 100644
--- a/test/system/030-run.bats
+++ b/test/system/030-run.bats
@@ -756,4 +756,14 @@ EOF
is "$output" ".*TERM=abc" "missing TERM environment variable despite TERM being set on commandline"
}
+@test "podman run - no /etc/hosts" {
+ skip_if_rootless "cannot move /etc/hosts file as a rootless user"
+ tmpfile=$PODMAN_TMPDIR/hosts
+ mv /etc/hosts $tmpfile
+ run_podman '?' run --rm --add-host "foo.com:1.2.3.4" $IMAGE cat "/etc/hosts"
+ mv $tmpfile /etc/hosts
+ is "$status" 0 "podman run without /etc/hosts file should work"
+ is "$output" "1.2.3.4 foo.com.*" "users can add hosts even without /etc/hosts"
+}
+
# vim: filetype=sh