Skip to content

Commit

Permalink
add base_hosts_file field to containers.conf
Browse files Browse the repository at this point in the history
base_hosts_file can be used to overwrite the default base host file
/etc/hosts which is used to copy hosts entries from this file into the
containers /etc/hosts file. As special value "image" can be used to copy
the entires from the image hosts file or an empty string "" to not use a
base file at all.

Ref containers/podman#13277
Ref containers/podman#13748

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Apr 11, 2022
1 parent 954f444 commit 67602a2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions docs/containers.conf.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Example: "run.oci.keep_original_groups=1"
Used to change the name of the default AppArmor profile of container engines.
The default profile name is "container-default".

**base_hosts_file**="/etc/hosts"

The hosts entries from the base hosts file are added to the containers hosts
file. This must be either an absolute path or as special values "image" which
uses the hosts file from the container image or an empty string "" which means
no base hosts file is used. The default is "/etc/hosts".

**cgroups**="enabled"

Determines whether the container will create CGroups.
Expand Down
6 changes: 3 additions & 3 deletions libnetwork/etchosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"io"
"os"
"strings"

"github.com/containers/common/pkg/config"
)

const (
// DefaultHostsFile is the default path to the hosts file
DefaultHostsFile = "/etc/hosts"
hostContainersInternal = "host.containers.internal"
localhost = "localhost"
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func parseHostsFile(file string) (HostEntries, error) {
if err != nil {
// do not error when the default hosts file does not exists
// https://github.com/containers/podman/issues/12667
if errors.Is(err, os.ErrNotExist) && file == DefaultHostsFile {
if errors.Is(err, os.ErrNotExist) && file == config.DefaultHostsFile {
return nil, nil
}
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ type ContainersConfig struct {
// Annotation to add to all containers
Annotations []string `toml:"annotations,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 used the /etc/hosts file from within the image.
BaseHostsFile string `toml:"base_hosts_file,omitempty"`

// Default way to create a cgroup namespace for the container
CgroupNS string `toml:"cgroupns,omitempty"`

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ = Describe("Config", func() {
// Then
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(defaultConfig.Containers.ApparmorProfile).To(gomega.Equal(apparmor.Profile))
gomega.Expect(defaultConfig.Containers.BaseHostsFile).To(gomega.Equal("/etc/hosts"))
gomega.Expect(defaultConfig.Containers.PidsLimit).To(gomega.BeEquivalentTo(2048))
gomega.Expect(defaultConfig.Engine.ServiceTimeout).To(gomega.BeEquivalentTo(5))
gomega.Expect(defaultConfig.NetNS()).To(gomega.BeEquivalentTo("private"))
Expand Down Expand Up @@ -362,6 +363,7 @@ image_copy_tmp_dir="storage"`
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(config.Containers.ApparmorProfile).To(gomega.Equal("container-default"))
gomega.Expect(config.Containers.PidsLimit).To(gomega.BeEquivalentTo(2048))
gomega.Expect(config.Containers.BaseHostsFile).To(gomega.BeEquivalentTo("/etc/hosts2"))
})

It("contents of passed-in file should override others", func() {
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/containers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
#
#apparmor_profile = "container-default"

# The hosts entries from the base hosts file are added to the containers hosts
# file. This must be either an absolute path or as special values "image" which
# uses the hosts file from the container image or an empty string "" which means
# no base hosts file is used. The default is "/etc/hosts".
#
#base_hosts_file = "/etc/hosts"

# Default way to to create a cgroup namespace for the container
# Options are:
# `private` Create private Cgroup Namespace for the container.
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ const (
CgroupfsCgroupsManager = "cgroupfs"
// DefaultApparmorProfile specifies the default apparmor profile for the container.
DefaultApparmorProfile = apparmor.Profile
// DefaultHostsFile is the default path to the hosts file
DefaultHostsFile = "/etc/hosts"
// SystemdCgroupsManager represents systemd native cgroup manager
SystemdCgroupsManager = "systemd"
// DefaultLogSizeMax is the default value for the maximum log size
Expand Down Expand Up @@ -187,6 +189,7 @@ func DefaultConfig() (*Config, error) {
Volumes: []string{},
Annotations: []string{},
ApparmorProfile: DefaultApparmorProfile,
BaseHostsFile: DefaultHostsFile,
CgroupNS: cgroupNS,
Cgroups: "enabled",
DefaultCapabilities: DefaultCapabilities,
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/testdata/containers_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ devices = [
# profile name is "container-default".
apparmor_profile = "container-default"

base_hosts_file = "/etc/hosts2"

# List of default capabilities for containers. If it is empty or commented out,
# only the capabilities defined in the containers json file by the user/kube
# will be added.
Expand Down

0 comments on commit 67602a2

Please sign in to comment.