diff options
author | baude <bbaude@redhat.com> | 2017-12-13 10:50:11 -0600 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-12-14 01:12:10 +0000 |
commit | f82030941344f7993f8f7018cb601847ec2dfd08 (patch) | |
tree | 5adcc30058b9549f345e734e13439323c874fc4d | |
parent | 5330d3da7c07861bfca12c7e8197a17a7c6a1b39 (diff) | |
download | podman-f82030941344f7993f8f7018cb601847ec2dfd08.tar.gz podman-f82030941344f7993f8f7018cb601847ec2dfd08.tar.bz2 podman-f82030941344f7993f8f7018cb601847ec2dfd08.zip |
Copy resolv.conf into container
For DNS to work properly, we need to copy the host's /etc/resolv.conf
into the container during Init(). We do this by copying it into the
containers rundir and then bind mounting it into the container.
Signed-off-by: baude <bbaude@redhat.com>
Closes: #130
Approved by: baude
-rw-r--r-- | libpod/container.go | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/libpod/container.go b/libpod/container.go index 604b5fe10..ddd986b90 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -19,6 +19,7 @@ import ( "github.com/docker/docker/pkg/namesgenerator" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/term" + "github.com/mrunalp/fileutils" spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-tools/generate" "github.com/opencontainers/selinux/go-selinux/label" @@ -558,10 +559,25 @@ func (c *Container) Init() (err error) { } } + // Copy /etc/resolv.conf to the container's rundir + runDirResolv, err := c.copyHostFileToRundir("/etc/resolv.conf") + if err != nil { + return errors.Wrapf(err, "unable to copy /etc/resolv.conf to ", runDirResolv) + } + // Copy /etc/hosts to the container's rundir + runDirHosts, err := c.copyHostFileToRundir("/etc/hosts") + if err != nil { + return errors.Wrapf(err, "unable to copy /etc/hosts to ", runDirHosts) + } + // Save OCI spec to disk g := generate.NewFromSpec(c.config.Spec) // Mount ShmDir from host into container g.AddBindMount(c.config.ShmDir, "/dev/shm", []string{"rw"}) + // Bind mount resolv.conf + g.AddBindMount(runDirResolv, "/etc/resolv.conf", []string{"rw"}) + // Bind mount hosts + g.AddBindMount(runDirHosts, "/etc/hosts", []string{"rw"}) c.runningSpec = g.Spec() c.runningSpec.Root.Path = c.state.Mountpoint c.runningSpec.Annotations[crioAnnotations.Created] = c.config.CreatedTime.Format(time.RFC3339Nano) @@ -1053,3 +1069,16 @@ func (c *Container) cleanupStorage() error { func (c *Container) CGroupPath() cgroups.Path { return cgroups.StaticPath(filepath.Join(CGroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID()))) } + +// copyHostFileToRundir copies the provided file to the runtimedir +func (c *Container) copyHostFileToRundir(sourcePath string) (string, error) { + destFileName := filepath.Join(c.state.RunDir, filepath.Base(sourcePath)) + if err := fileutils.CopyFile(sourcePath, destFileName); err != nil { + return "", err + } + // Relabel runDirResolv for the container + if err := label.Relabel(destFileName, c.config.MountLabel, false); err != nil { + return "", err + } + return destFileName, nil +} |