From 3b691d085c6be693e8c212b56b571a703488f693 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Sat, 22 Apr 2017 02:54:29 +0200 Subject: [PATCH] container: Bind mount hosts file for host networking containers Fixes #451 Signed-off-by: Samuel Ortiz --- server/container_create.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/container_create.go b/server/container_create.go index aa11a83e..3136268a 100644 --- a/server/container_create.go +++ b/server/container_create.go @@ -175,6 +175,15 @@ func setupContainerUser(specgen *generate.Generator, rootfs string, sc *pb.Linux return nil } +func hostNetwork(containerConfig *pb.ContainerConfig) bool { + securityContext := containerConfig.GetLinux().GetSecurityContext() + if securityContext == nil || securityContext.GetNamespaceOptions() == nil { + return false + } + + return securityContext.GetNamespaceOptions().HostNetwork +} + // ensureSaneLogPath is a hack to fix https://issues.k8s.io/44043 which causes // logPath to be a broken symlink to some magical Docker path. Ideally we // wouldn't have to deal with this, but until that issue is fixed we have to @@ -455,6 +464,11 @@ func (s *Server) createSandboxContainer(ctx context.Context, containerID string, specgen.AddBindMount(sb.resolvPath, "/etc/resolv.conf", []string{"ro"}) } + // Bind mount /etc/hosts for host networking containers + if hostNetwork(containerConfig) { + specgen.AddBindMount("/etc/hosts", "/etc/hosts", []string{"ro"}) + } + if sb.hostname != "" { specgen.SetHostname(sb.hostname) }