From 10c88fc3aba642e2c084e976aa7ec44cead2b4b6 Mon Sep 17 00:00:00 2001 From: Yuanhong Peng Date: Fri, 4 Aug 2017 12:13:14 +0800 Subject: [PATCH] Ignore "no such file" error when docker cannot find `resolv.conf` `/etc/resolv.conf` is not an essential file in filesystem. (see http://man7.org/linux/man-pages/man5/resolv.conf.5.html) > If this file does not exist, only the name server on the local machine > will be queried It's baffling to users that containers can start with an empty `resolv.conf` but cannot without this file. This PR: * ignore this error and use default servers for containers in `bridge` mode networking. * create an empty resolv.conf in `/var/lib/docker/containers/` in `host` mode networking. Signed-off-by: Yuanhong Peng --- libnetwork/sandbox_dns_unix.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libnetwork/sandbox_dns_unix.go b/libnetwork/sandbox_dns_unix.go index f18f0b3ee6..d196330558 100644 --- a/libnetwork/sandbox_dns_unix.go +++ b/libnetwork/sandbox_dns_unix.go @@ -197,14 +197,26 @@ func (sb *sandbox) setupDNS() error { // This is for the host mode networking if sb.config.originResolvConfPath != "" { if err := copyFile(sb.config.originResolvConfPath, sb.config.resolvConfPath); err != nil { - return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err) + if !os.IsNotExist(err) { + return fmt.Errorf("could not copy source resolv.conf file %s to %s: %v", sb.config.originResolvConfPath, sb.config.resolvConfPath, err) + } + logrus.Infof("%s does not exist, we create an empty resolv.conf for container", sb.config.originResolvConfPath) + if err := createFile(sb.config.resolvConfPath); err != nil { + return err + } } return nil } currRC, err := resolvconf.Get() if err != nil { - return err + if !os.IsNotExist(err) { + return err + } + // it's ok to continue if /etc/resolv.conf doesn't exist, default resolvers (Google's Public DNS) + // will be used + currRC = &resolvconf.File{} + logrus.Infof("/etc/resolv.conf does not exist") } if len(sb.config.dnsList) > 0 || len(sb.config.dnsSearchList) > 0 || len(sb.config.dnsOptionsList) > 0 {