Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support hosts without /etc/hosts #12668

Merged
merged 1 commit into from
Dec 22, 2021
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
31 changes: 19 additions & 12 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/system/030-run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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