浏览代码

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/<id>` in
  `host` mode networking.

Signed-off-by: Yuanhong Peng <pengyuanhong@huawei.com>
Yuanhong Peng 8 年之前
父节点
当前提交
10c88fc3ab
共有 1 个文件被更改,包括 14 次插入2 次删除
  1. 14 2
      libnetwork/sandbox_dns_unix.go

+ 14 - 2
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 {