mirror of
https://github.com/containers/podman.git
synced 2025-05-17 23:26:08 +08:00
Add --hosts-file flag to container and pod commands
* Add --hosts-file flag to container create, container run and pod create * Add HostsFile field to pod inspect and container inspect results * Test BaseHostsFile config in containers.conf Signed-off-by: Gavin Lam <gavin.oss@tutamail.com>
This commit is contained in:
@ -716,6 +716,13 @@ func AutocompleteNetworks(cmd *cobra.Command, args []string, toComplete string)
|
||||
return getNetworks(cmd, toComplete, completeDefault)
|
||||
}
|
||||
|
||||
// AutocompleteHostsFile - Autocomplete hosts file options.
|
||||
// -> "image", "none", paths
|
||||
func AutocompleteHostsFile(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
hostsFileModes := []string{"image", "none"}
|
||||
return hostsFileModes, cobra.ShellCompDirectiveDefault
|
||||
}
|
||||
|
||||
// AutocompleteDefaultOneArg - Autocomplete path only for the first argument.
|
||||
func AutocompleteDefaultOneArg(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||
if len(args) == 0 {
|
||||
|
@ -26,6 +26,13 @@ func DefineNetFlags(cmd *cobra.Command) {
|
||||
)
|
||||
_ = cmd.RegisterFlagCompletionFunc(addHostFlagName, completion.AutocompleteNone)
|
||||
|
||||
hostsFileFlagName := "hosts-file"
|
||||
netFlags.String(
|
||||
hostsFileFlagName, "",
|
||||
`Base file to create the /etc/hosts file inside the container, or one of the special values. ("image"|"none")`,
|
||||
)
|
||||
_ = cmd.RegisterFlagCompletionFunc(hostsFileFlagName, AutocompleteHostsFile)
|
||||
|
||||
dnsFlagName := "dns"
|
||||
netFlags.StringSlice(
|
||||
dnsFlagName, podmanConfig.ContainersConf.DNSServers(),
|
||||
@ -116,6 +123,13 @@ func NetFlagsToNetOptions(opts *entities.NetOptions, flags pflag.FlagSet) (*enti
|
||||
}
|
||||
}
|
||||
|
||||
if flags.Changed("hosts-file") {
|
||||
opts.HostsFile, err = flags.GetString("hosts-file")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if flags.Changed("dns") {
|
||||
servers, err := flags.GetStringSlice("dns")
|
||||
if err != nil {
|
||||
|
@ -323,6 +323,9 @@ func CreateInit(c *cobra.Command, vals entities.ContainerCreateOptions, isInfra
|
||||
if noHosts && c.Flag("add-host").Changed {
|
||||
return vals, errors.New("--no-hosts and --add-host cannot be set together")
|
||||
}
|
||||
if noHosts && c.Flag("hosts-file").Changed {
|
||||
return vals, errors.New("--no-hosts and --hosts-file cannot be set together")
|
||||
}
|
||||
|
||||
if !isInfra && c.Flag("entrypoint").Changed {
|
||||
val := c.Flag("entrypoint").Value.String()
|
||||
|
@ -265,6 +265,7 @@ func create(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
podSpec.InfraContainerSpec = specgen.NewSpecGenerator(imageName, false)
|
||||
podSpec.InfraContainerSpec.RawImageName = rawImageName
|
||||
podSpec.InfraContainerSpec.BaseHostsFile = podSpec.PodNetworkConfig.HostsFile
|
||||
podSpec.InfraContainerSpec.NetworkOptions = podSpec.NetworkOptions
|
||||
podSpec.InfraContainerSpec.RestartPolicy = podSpec.RestartPolicy
|
||||
err = specgenutil.FillOutSpecGen(podSpec.InfraContainerSpec, &infraOptions, []string{})
|
||||
|
12
docs/source/markdown/options/hosts-file.md
Normal file
12
docs/source/markdown/options/hosts-file.md
Normal file
@ -0,0 +1,12 @@
|
||||
####> This option file is used in:
|
||||
####> podman create, pod create, run
|
||||
####> If file is edited, make sure the changes
|
||||
####> are applicable to all of those.
|
||||
#### **--hosts-file**=*path* | *none* | *image*
|
||||
|
||||
Base file to create the `/etc/hosts` file inside the container. This must either
|
||||
be an absolute path to a file on the host system, or one of the following
|
||||
special flags:
|
||||
"" Follow the `base_hosts_file` configuration in _containers.conf_ (the default)
|
||||
`none` Do not use a base file (i.e. start with an empty file)
|
||||
`image` Use the container image's `/etc/hosts` file as base file
|
@ -199,6 +199,8 @@ Print usage statement
|
||||
|
||||
@@option hostname.container
|
||||
|
||||
@@option hosts-file
|
||||
|
||||
@@option hostuser
|
||||
|
||||
@@option http-proxy
|
||||
|
@ -88,6 +88,8 @@ Print usage statement.
|
||||
|
||||
@@option hostname.pod
|
||||
|
||||
@@option hosts-file
|
||||
|
||||
#### **--infra**
|
||||
|
||||
Create an infra container and associate it with the pod. An infra container is a lightweight container used to coordinate the shared kernel namespace of a pod. Default: true.
|
||||
|
@ -233,6 +233,8 @@ Print usage statement
|
||||
|
||||
@@option hostname.container
|
||||
|
||||
@@option hosts-file
|
||||
|
||||
@@option hostuser
|
||||
|
||||
@@option http-proxy
|
||||
|
@ -290,11 +290,10 @@ type ContainerNetworkConfig struct {
|
||||
// bind-mounted inside the container.
|
||||
// Conflicts with HostAdd.
|
||||
UseImageHosts bool
|
||||
// BaseHostsFile is the path to a hosts file, the entries from this file
|
||||
// are added to the containers hosts file. As special value "image" is
|
||||
// allowed which uses the /etc/hosts file from within the image and "none"
|
||||
// which uses no base file at all. If it is empty we should default
|
||||
// to the base_hosts_file configuration in containers.conf.
|
||||
// BaseHostsFile is the base file to create the `/etc/hosts` file inside the container.
|
||||
// This must either be an absolute path to a file on the host system, or one of the
|
||||
// special flags `image` or `none`.
|
||||
// If it is empty it defaults to the base_hosts_file configuration in containers.conf.
|
||||
BaseHostsFile string `json:"baseHostsFile,omitempty"`
|
||||
// Hosts to add in container
|
||||
// Will be appended to host's host file
|
||||
|
@ -543,6 +543,8 @@ func (c *Container) generateInspectContainerHostConfig(ctrSpec *spec.Spec, named
|
||||
hostConfig.GroupAdd = make([]string, 0, len(c.config.Groups))
|
||||
hostConfig.GroupAdd = append(hostConfig.GroupAdd, c.config.Groups...)
|
||||
|
||||
hostConfig.HostsFile = c.config.BaseHostsFile
|
||||
|
||||
if ctrSpec.Process != nil {
|
||||
if ctrSpec.Process.OOMScoreAdj != nil {
|
||||
hostConfig.OomScoreAdj = *ctrSpec.Process.OOMScoreAdj
|
||||
|
@ -443,6 +443,8 @@ type InspectContainerHostConfig struct {
|
||||
// ExtraHosts contains hosts that will be added to the container's
|
||||
// /etc/hosts.
|
||||
ExtraHosts []string `json:"ExtraHosts"`
|
||||
// HostsFile is the base file to create the `/etc/hosts` file inside the container.
|
||||
HostsFile string `json:"HostsFile"`
|
||||
// GroupAdd contains groups that the user inside the container will be
|
||||
// added to.
|
||||
GroupAdd []string `json:"GroupAdd"`
|
||||
|
@ -124,6 +124,9 @@ type InspectPodInfraConfig struct {
|
||||
// HostAdd adds a number of hosts to the infra container's resolv.conf
|
||||
// which will be shared with the rest of the pod.
|
||||
HostAdd []string
|
||||
// HostsFile is the base file to create the `/etc/hosts` file inside the infra container
|
||||
// which will be shared with the rest of the pod.
|
||||
HostsFile string
|
||||
// Networks is a list of networks the pod will join.
|
||||
Networks []string
|
||||
// NetworkOptions are additional options for each network
|
||||
|
@ -709,6 +709,9 @@ func (p *Pod) Inspect() (*define.InspectPodData, error) {
|
||||
infraConfig.HostAdd = make([]string, 0, len(infra.config.HostAdd))
|
||||
infraConfig.HostAdd = append(infraConfig.HostAdd, infra.config.HostAdd...)
|
||||
}
|
||||
if len(infra.config.BaseHostsFile) > 0 {
|
||||
infraConfig.HostsFile = infra.config.BaseHostsFile
|
||||
}
|
||||
|
||||
networks, err := infra.networks()
|
||||
if err != nil {
|
||||
|
@ -368,6 +368,7 @@ func ToPodSpecGen(s specgen.PodSpecGenerator, p *PodCreateOptions) (*specgen.Pod
|
||||
s.DNSOption = p.Net.DNSOptions
|
||||
s.NoManageHosts = p.Net.NoHosts
|
||||
s.HostAdd = p.Net.AddHosts
|
||||
s.HostsFile = p.Net.HostsFile
|
||||
}
|
||||
|
||||
// Cgroup
|
||||
|
@ -55,6 +55,7 @@ type NetOptions struct {
|
||||
DNSOptions []string `json:"dns_option,omitempty"`
|
||||
DNSSearch []string `json:"dns_search,omitempty"`
|
||||
DNSServers []net.IP `json:"dns_server,omitempty"`
|
||||
HostsFile string `json:"hosts_file,omitempty"`
|
||||
Network specgen.Namespace `json:"netns,omitempty"`
|
||||
NoHosts bool `json:"no_manage_hosts,omitempty"`
|
||||
PublishPorts []types.PortMapping `json:"portmappings,omitempty"`
|
||||
|
@ -243,6 +243,9 @@ func MapSpec(p *specgen.PodSpecGenerator) (*specgen.SpecGenerator, error) {
|
||||
if len(p.HostAdd) > 0 {
|
||||
spec.HostAdd = p.HostAdd
|
||||
}
|
||||
if len(p.HostsFile) > 0 {
|
||||
spec.BaseHostsFile = p.HostsFile
|
||||
}
|
||||
if len(p.DNSServer) > 0 {
|
||||
var dnsServers []net.IP
|
||||
dnsServers = append(dnsServers, p.DNSServer...)
|
||||
|
@ -58,6 +58,9 @@ func (p *PodSpecGenerator) Validate() error {
|
||||
if len(p.HostAdd) > 0 {
|
||||
return exclusivePodOptions("NoInfra", "HostAdd")
|
||||
}
|
||||
if len(p.HostsFile) > 0 {
|
||||
return exclusivePodOptions("NoInfra", "HostsFile")
|
||||
}
|
||||
if p.NoManageResolvConf {
|
||||
return exclusivePodOptions("NoInfra", "NoManageResolvConf")
|
||||
}
|
||||
@ -79,8 +82,13 @@ func (p *PodSpecGenerator) Validate() error {
|
||||
return exclusivePodOptions("NoManageResolvConf", "DNSOption")
|
||||
}
|
||||
}
|
||||
if p.NoManageHosts && len(p.HostAdd) > 0 {
|
||||
return exclusivePodOptions("NoManageHosts", "HostAdd")
|
||||
if p.NoManageHosts {
|
||||
if len(p.HostAdd) > 0 {
|
||||
return exclusivePodOptions("NoManageHosts", "HostAdd")
|
||||
}
|
||||
if len(p.HostsFile) > 0 {
|
||||
return exclusivePodOptions("NoManageHosts", "HostsFile")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -170,6 +170,13 @@ type PodNetworkConfig struct {
|
||||
// Conflicts with NoInfra=true and NoManageHosts.
|
||||
// Optional.
|
||||
HostAdd []string `json:"hostadd,omitempty"`
|
||||
// HostsFile is the base file to create the `/etc/hosts` file inside the infra container.
|
||||
// This must either be an absolute path to a file on the host system, or one of the
|
||||
// special flags `image` or `none`.
|
||||
// If it is empty it defaults to the base_hosts_file configuration in containers.conf.
|
||||
// Conflicts with NoInfra=true and NoManageHosts.
|
||||
// Optional.
|
||||
HostsFile string `json:"hostsFile,omitempty"`
|
||||
// NetworkOptions are additional options for each network
|
||||
// Optional.
|
||||
NetworkOptions map[string][]string `json:"network_options,omitempty"`
|
||||
|
@ -539,11 +539,10 @@ type ContainerNetworkConfig struct {
|
||||
// Conflicts with HostAdd.
|
||||
// Optional.
|
||||
UseImageHosts *bool `json:"use_image_hosts,omitempty"`
|
||||
// BaseHostsFile is the path to a hosts file, the entries from this file
|
||||
// are added to the containers hosts file. As special value "image" is
|
||||
// allowed which uses the /etc/hosts file from within the image and "none"
|
||||
// which uses no base file at all. If it is empty we should default
|
||||
// to the base_hosts_file configuration in containers.conf.
|
||||
// BaseHostsFile is the base file to create the `/etc/hosts` file inside the container.
|
||||
// This must either be an absolute path to a file on the host system, or one of the
|
||||
// special flags `image` or `none`.
|
||||
// If it is empty it defaults to the base_hosts_file configuration in containers.conf.
|
||||
// Optional.
|
||||
BaseHostsFile string `json:"base_hosts_file,omitempty"`
|
||||
// HostAdd is a set of hosts which will be added to the container's
|
||||
|
@ -587,6 +587,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *entities.ContainerCreateOptions
|
||||
|
||||
if c.Net != nil {
|
||||
s.HostAdd = c.Net.AddHosts
|
||||
s.BaseHostsFile = c.Net.HostsFile
|
||||
s.UseImageResolvConf = &c.Net.UseImageResolvConf
|
||||
s.DNSServers = c.Net.DNSServers
|
||||
s.DNSSearch = c.Net.DNSSearch
|
||||
|
@ -663,14 +663,14 @@ func (p *PodmanTestIntegration) RunLsContainerInPod(name, pod string) (*PodmanSe
|
||||
|
||||
// BuildImage uses podman build and buildah to build an image
|
||||
// called imageName based on a string dockerfile
|
||||
func (p *PodmanTestIntegration) BuildImage(dockerfile, imageName string, layers string) string {
|
||||
return p.buildImage(dockerfile, imageName, layers, "")
|
||||
func (p *PodmanTestIntegration) BuildImage(dockerfile, imageName string, layers string, extraOptions ...string) string {
|
||||
return p.buildImage(dockerfile, imageName, layers, "", extraOptions)
|
||||
}
|
||||
|
||||
// BuildImageWithLabel uses podman build and buildah to build an image
|
||||
// called imageName based on a string dockerfile, adds desired label to paramset
|
||||
func (p *PodmanTestIntegration) BuildImageWithLabel(dockerfile, imageName string, layers string, label string) string {
|
||||
return p.buildImage(dockerfile, imageName, layers, label)
|
||||
func (p *PodmanTestIntegration) BuildImageWithLabel(dockerfile, imageName string, layers string, label string, extraOptions ...string) string {
|
||||
return p.buildImage(dockerfile, imageName, layers, label, extraOptions)
|
||||
}
|
||||
|
||||
// PodmanPID execs podman and returns its PID
|
||||
@ -1299,7 +1299,7 @@ func (s *PodmanSessionIntegration) jq(jqCommand string) (string, error) {
|
||||
return strings.TrimRight(out.String(), "\n"), err
|
||||
}
|
||||
|
||||
func (p *PodmanTestIntegration) buildImage(dockerfile, imageName string, layers string, label string) string {
|
||||
func (p *PodmanTestIntegration) buildImage(dockerfile, imageName string, layers string, label string, extraOptions []string) string {
|
||||
dockerfilePath := filepath.Join(p.TempDir, "Dockerfile-"+stringid.GenerateRandomID())
|
||||
err := os.WriteFile(dockerfilePath, []byte(dockerfile), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
@ -1310,6 +1310,9 @@ func (p *PodmanTestIntegration) buildImage(dockerfile, imageName string, layers
|
||||
if len(imageName) > 0 {
|
||||
cmd = append(cmd, []string{"-t", imageName}...)
|
||||
}
|
||||
if len(extraOptions) > 0 {
|
||||
cmd = append(cmd, extraOptions...)
|
||||
}
|
||||
cmd = append(cmd, p.TempDir)
|
||||
session := p.Podman(cmd)
|
||||
session.Wait(240)
|
||||
|
@ -21,7 +21,9 @@ import (
|
||||
var _ = Describe("Verify podman containers.conf usage", func() {
|
||||
|
||||
BeforeEach(func() {
|
||||
os.Setenv("CONTAINERS_CONF", "config/containers.conf")
|
||||
confPath, err := filepath.Abs("config/containers.conf")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
os.Setenv("CONTAINERS_CONF", confPath)
|
||||
if IsRemote() {
|
||||
podmanTest.RestartRemoteService()
|
||||
}
|
||||
@ -463,6 +465,79 @@ var _ = Describe("Verify podman containers.conf usage", func() {
|
||||
Expect(session.OutputToString()).To(ContainSubstring("test"))
|
||||
})
|
||||
|
||||
Describe("base_hosts_file in containers.conf", func() {
|
||||
var baseHostsFile string
|
||||
var session *PodmanSessionIntegration
|
||||
|
||||
JustBeforeEach(func() {
|
||||
conffile := filepath.Join(podmanTest.TempDir, "containers.conf")
|
||||
err = os.WriteFile(conffile, []byte(fmt.Sprintf("[containers]\nbase_hosts_file=\"%s\"\nno_hosts=false\n", baseHostsFile)), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
os.Setenv("CONTAINERS_CONF_OVERRIDE", conffile)
|
||||
if IsRemote() {
|
||||
podmanTest.RestartRemoteService()
|
||||
}
|
||||
|
||||
dockerfile := strings.Join([]string{
|
||||
`FROM quay.io/libpod/alpine:latest`,
|
||||
`RUN echo '56.78.12.34 image.example.com' > /etc/hosts`,
|
||||
}, "\n")
|
||||
podmanTest.BuildImage(dockerfile, "foobar.com/hosts_test:latest", "false", "--no-hosts")
|
||||
|
||||
session = podmanTest.Podman([]string{"run", "--name", "hosts_test", "--hostname", "hosts_test.dev", "--rm", "foobar.com/hosts_test:latest", "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
})
|
||||
|
||||
Describe("base_hosts_file=path", func() {
|
||||
BeforeEach(func() {
|
||||
hostsPath := filepath.Join(podmanTest.TempDir, "hosts")
|
||||
err := os.WriteFile(hostsPath, []byte("12.34.56.78 file.example.com"), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
baseHostsFile = hostsPath
|
||||
})
|
||||
|
||||
It("should use the hosts file from the file path", func() {
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("12.34.56.78 file.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("base_hosts_file=image", func() {
|
||||
BeforeEach(func() {
|
||||
baseHostsFile = "image"
|
||||
})
|
||||
|
||||
It("should use the hosts file from the container image", func() {
|
||||
Expect(session.OutputToString()).To(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 file.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("base_hosts_file=none", func() {
|
||||
BeforeEach(func() {
|
||||
baseHostsFile = "none"
|
||||
})
|
||||
|
||||
It("should not use any hosts files", func() {
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 file.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
It("seccomp profile path", func() {
|
||||
configPath := filepath.Join(podmanTest.TempDir, "containers.conf")
|
||||
os.Setenv("CONTAINERS_CONF", configPath)
|
||||
|
@ -149,6 +149,130 @@ var _ = Describe("Podman pod create", func() {
|
||||
Expect(podCreate).Should(ExitWithError(125, "NoInfra and HostAdd are mutually exclusive pod options: invalid pod spec"))
|
||||
})
|
||||
|
||||
It("podman create pod with --add-host and --no-hosts should fail", func() {
|
||||
name := "test"
|
||||
podCreate := podmanTest.Podman([]string{"pod", "create", "--add-host", "test.example.com:12.34.56.78", "--name", name, "--no-hosts"})
|
||||
podCreate.WaitWithDefaultTimeout()
|
||||
Expect(podCreate).Should(ExitWithError(125, "--no-hosts and --add-host cannot be set together"))
|
||||
})
|
||||
|
||||
Describe("podman create pod with --hosts-file", func() {
|
||||
BeforeEach(func() {
|
||||
imageHosts := filepath.Join(podmanTest.TempDir, "pause_hosts")
|
||||
err := os.WriteFile(imageHosts, []byte("56.78.12.34 image.example.com"), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
configHosts := filepath.Join(podmanTest.TempDir, "hosts")
|
||||
err = os.WriteFile(configHosts, []byte("12.34.56.78 config.example.com"), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
confFile := filepath.Join(podmanTest.TempDir, "containers.conf")
|
||||
err = os.WriteFile(confFile, []byte(fmt.Sprintf("[containers]\nbase_hosts_file=\"%s\"\n", configHosts)), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
os.Setenv("CONTAINERS_CONF_OVERRIDE", confFile)
|
||||
if IsRemote() {
|
||||
podmanTest.RestartRemoteService()
|
||||
}
|
||||
|
||||
dockerfile := strings.Join([]string{
|
||||
`FROM ` + INFRA_IMAGE,
|
||||
`COPY pause_hosts /etc/hosts`,
|
||||
}, "\n")
|
||||
podmanTest.BuildImage(dockerfile, "foobar.com/hosts_test_pause:latest", "false", "--no-hosts")
|
||||
})
|
||||
|
||||
It("--hosts-file=path", func() {
|
||||
hostsPath := filepath.Join(podmanTest.TempDir, "hosts")
|
||||
err := os.WriteFile(hostsPath, []byte("23.45.67.89 file.example.com"), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
podCreate := podmanTest.Podman([]string{"pod", "create", "--hostname", "hosts_test.dev", "--hosts-file=" + hostsPath, "--add-host=add.example.com:34.56.78.90", "--infra-image=foobar.com/hosts_test_pause:latest", "--infra-name=hosts_test_infra", "--name", "hosts_test_pod"})
|
||||
podCreate.WaitWithDefaultTimeout()
|
||||
Expect(podCreate).Should(ExitCleanly())
|
||||
|
||||
session := podmanTest.Podman([]string{"run", "--pod", "hosts_test_pod", "--name", "hosts_test", "--rm", ALPINE, "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("23.45.67.89 file.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test_infra"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 hosts_test"))
|
||||
})
|
||||
|
||||
It("--hosts-file=image", func() {
|
||||
podCreate := podmanTest.Podman([]string{"pod", "create", "--hostname", "hosts_test.dev", "--hosts-file=image", "--add-host=add.example.com:34.56.78.90", "--infra-image=foobar.com/hosts_test_pause:latest", "--infra-name=hosts_test_infra", "--name", "hosts_test_pod"})
|
||||
podCreate.WaitWithDefaultTimeout()
|
||||
Expect(podCreate).Should(ExitCleanly())
|
||||
|
||||
session := podmanTest.Podman([]string{"run", "--pod", "hosts_test_pod", "--name", "hosts_test", "--rm", ALPINE, "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).To(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test_infra"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 hosts_test"))
|
||||
})
|
||||
|
||||
It("--hosts-file=none", func() {
|
||||
podCreate := podmanTest.Podman([]string{"pod", "create", "--hostname", "hosts_test.dev", "--hosts-file=none", "--add-host=add.example.com:34.56.78.90", "--infra-image=foobar.com/hosts_test_pause:latest", "--infra-name=hosts_test_infra", "--name", "hosts_test_pod"})
|
||||
podCreate.WaitWithDefaultTimeout()
|
||||
Expect(podCreate).Should(ExitCleanly())
|
||||
|
||||
session := podmanTest.Podman([]string{"run", "--pod", "hosts_test_pod", "--name", "hosts_test", "--rm", ALPINE, "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test_infra"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 hosts_test"))
|
||||
})
|
||||
|
||||
It("--hosts-file= falls back to containers.conf", func() {
|
||||
podCreate := podmanTest.Podman([]string{"pod", "create", "--hostname", "hosts_test.dev", "--hosts-file=", "--add-host=add.example.com:34.56.78.90", "--infra-image=foobar.com/hosts_test_pause:latest", "--infra-name=hosts_test_infra", "--name", "hosts_test_pod"})
|
||||
podCreate.WaitWithDefaultTimeout()
|
||||
Expect(podCreate).Should(ExitCleanly())
|
||||
|
||||
session := podmanTest.Podman([]string{"run", "--pod", "hosts_test_pod", "--name", "hosts_test", "--rm", ALPINE, "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test_infra"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 hosts_test"))
|
||||
})
|
||||
})
|
||||
|
||||
It("podman create pod with --hosts-file and no infra should fail", func() {
|
||||
name := "test"
|
||||
podCreate := podmanTest.Podman([]string{"pod", "create", "--hosts-file=image", "--name", name, "--infra=false"})
|
||||
podCreate.WaitWithDefaultTimeout()
|
||||
Expect(podCreate).Should(ExitWithError(125, "NoInfra and HostsFile are mutually exclusive pod options: invalid pod spec"))
|
||||
})
|
||||
|
||||
It("podman create pod with --hosts-file and --no-hosts should fail", func() {
|
||||
name := "test"
|
||||
podCreate := podmanTest.Podman([]string{"pod", "create", "--hosts-file=image", "--name", name, "--no-hosts"})
|
||||
podCreate.WaitWithDefaultTimeout()
|
||||
Expect(podCreate).Should(ExitWithError(125, "--no-hosts and --hosts-file cannot be set together"))
|
||||
})
|
||||
|
||||
It("podman create pod with DNS server set", func() {
|
||||
name := "test"
|
||||
server := "12.34.56.78"
|
||||
|
@ -1504,6 +1504,112 @@ VOLUME %s`, ALPINE, volPath, volPath)
|
||||
Expect(session).To(ExitWithError(125, "--no-hosts and --add-host cannot be set together"))
|
||||
})
|
||||
|
||||
Describe("podman run with --hosts-file", func() {
|
||||
BeforeEach(func() {
|
||||
configHosts := filepath.Join(podmanTest.TempDir, "hosts")
|
||||
err := os.WriteFile(configHosts, []byte("12.34.56.78 config.example.com"), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
confFile := filepath.Join(podmanTest.TempDir, "containers.conf")
|
||||
err = os.WriteFile(confFile, []byte(fmt.Sprintf("[containers]\nbase_hosts_file=\"%s\"\n", configHosts)), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
os.Setenv("CONTAINERS_CONF_OVERRIDE", confFile)
|
||||
if IsRemote() {
|
||||
podmanTest.RestartRemoteService()
|
||||
}
|
||||
|
||||
dockerfile := strings.Join([]string{
|
||||
`FROM quay.io/libpod/alpine:latest`,
|
||||
`RUN echo '56.78.12.34 image.example.com' > /etc/hosts`,
|
||||
}, "\n")
|
||||
podmanTest.BuildImage(dockerfile, "foobar.com/hosts_test:latest", "false", "--no-hosts")
|
||||
})
|
||||
|
||||
It("--hosts-file=path", func() {
|
||||
hostsPath := filepath.Join(podmanTest.TempDir, "hosts")
|
||||
err := os.WriteFile(hostsPath, []byte("23.45.67.89 file.example.com"), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
session := podmanTest.Podman([]string{"run", "--hostname", "hosts_test.dev", "--hosts-file=" + hostsPath, "--add-host=add.example.com:34.56.78.90", "--name", "hosts_test", "--rm", "foobar.com/hosts_test:latest", "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("23.45.67.89 file.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
|
||||
It("--hosts-file=image", func() {
|
||||
session := podmanTest.Podman([]string{"run", "--hostname", "hosts_test.dev", "--hosts-file=image", "--add-host=add.example.com:34.56.78.90", "--name", "hosts_test", "--rm", "foobar.com/hosts_test:latest", "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).To(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
|
||||
It("--hosts-file=none", func() {
|
||||
session := podmanTest.Podman([]string{"run", "--hostname", "hosts_test.dev", "--hosts-file=none", "--add-host=add.example.com:34.56.78.90", "--name", "hosts_test", "--rm", "foobar.com/hosts_test:latest", "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
|
||||
It("--hosts-file= falls back to containers.conf", func() {
|
||||
session := podmanTest.Podman([]string{"run", "--hostname", "hosts_test.dev", "--hosts-file=", "--add-host=add.example.com:34.56.78.90", "--name", "hosts_test", "--rm", "foobar.com/hosts_test:latest", "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
|
||||
It("works with pod without an infra-container", func() {
|
||||
_, ec, _ := podmanTest.CreatePod(map[string][]string{"--name": {"hosts_test_pod"}})
|
||||
Expect(ec).To(Equal(0))
|
||||
|
||||
session := podmanTest.Podman([]string{"run", "--pod", "hosts_test_pod", "--hostname", "hosts_test.dev", "--hosts-file=image", "--add-host=add.example.com:34.56.78.90", "--name", "hosts_test", "--rm", "foobar.com/hosts_test:latest", "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).Should(ExitCleanly())
|
||||
Expect(session.OutputToString()).To(ContainSubstring("56.78.12.34 image.example.com"))
|
||||
Expect(session.OutputToString()).ToNot(ContainSubstring("12.34.56.78 config.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("34.56.78.90 add.example.com"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("127.0.0.1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("::1 localhost"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("host.containers.internal host.docker.internal"))
|
||||
Expect(session.OutputToString()).To(ContainSubstring("hosts_test.dev hosts_test"))
|
||||
})
|
||||
|
||||
It("should fail with --no-hosts", func() {
|
||||
hostsPath := filepath.Join(podmanTest.TempDir, "hosts")
|
||||
err := os.WriteFile(hostsPath, []byte("23.45.67.89 file2.example.com"), 0755)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
session := podmanTest.Podman([]string{"run", "--no-hosts", "--hosts-file=" + hostsPath, "--name", "hosts_test", "--rm", "foobar.com/hosts_test:latest", "cat", "/etc/hosts"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session).To(ExitWithError(125, "--no-hosts and --hosts-file cannot be set together"))
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
It("podman run with restart-policy always restarts containers", func() {
|
||||
testDir := filepath.Join(podmanTest.RunRoot, "restart-test")
|
||||
err := os.MkdirAll(testDir, 0755)
|
||||
|
Reference in New Issue
Block a user