Skip to content

Commit

Permalink
Enable no_hosts from containers.conf
Browse files Browse the repository at this point in the history
Since we have no good way to enable this on the server side, we will
just allow it to be set on the client side. This should solve almost all
cases.

Partially fixes: containers#9500

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Feb 26, 2021
1 parent 05410e8 commit 680daca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/common/netflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func DefineNetFlags(cmd *cobra.Command) {
_ = cmd.RegisterFlagCompletionFunc(publishFlagName, completion.AutocompleteNone)

netFlags.Bool(
"no-hosts", false,
"no-hosts", containerConfig.Containers.NoHosts,
"Do not create /etc/hosts within the container, instead use the version from the image",
)
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/podman/containers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func createInit(c *cobra.Command) error {
return errors.Errorf("--cpu-quota and --cpus cannot be set together")
}

if c.Flag("no-hosts").Changed && c.Flag("add-host").Changed {
noHosts, err := c.Flags().GetBool("no-hosts")
if err != nil {
return err
}
if noHosts && c.Flag("add-host").Changed {
return errors.Errorf("--no-hosts and --add-host cannot be set together")
}
cliVals.UserNS = c.Flag("userns").Value.String()
Expand Down
1 change: 1 addition & 0 deletions test/e2e/config/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ umask = "0002"

annotations=["run.oci.keep_original_groups=1",]

no_hosts=true
[engine]

network_cmd_options=["allow_host_loopback=true"]
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/containers_conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,26 @@ var _ = Describe("Podman run", func() {
Expect(inspect.OutputToString()).To(ContainSubstring("run.oci.keep_original_groups:1"))
})

It("podman run with --add-host and no-hosts=true fails", func() {
session := podmanTest.Podman([]string{"run", "-dt", "--add-host", "test1:127.0.0.1", ALPINE, "top"})
session.WaitWithDefaultTimeout()
Expect(session).To(ExitWithError())
Expect(session.ErrorToString()).To(ContainSubstring("--no-hosts and --add-host cannot be set together"))

session = podmanTest.Podman([]string{"run", "-dt", "--add-host", "test1:127.0.0.1", "--no-hosts=false", ALPINE, "top"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
})

It("podman run with no-hosts=true /etc/hosts does not include hostname", func() {
session := podmanTest.Podman([]string{"run", "--rm", "--name", "test", ALPINE, "cat", "/etc/hosts"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(session.OutputToString()).To(Not(ContainSubstring("test")))

session = podmanTest.Podman([]string{"run", "--rm", "--name", "test", "--no-hosts=false", ALPINE, "cat", "/etc/hosts"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(session.OutputToString()).To(ContainSubstring("test"))
})
})

0 comments on commit 680daca

Please sign in to comment.