Browse code

Remove nameserver 127.0.0.1 line rather then dumping resolv.conf

We have a bug report complaining about docker dumping the contents of the
hosts resolv.conf if it container 127.0.0.1. They asked that instead
of dropping the file altogether, that we just remove the line.

This patch removes the 127.0.0.1 lines, if they exist and then
checks if any nameserver lines exist.

Docker-DCO-1.1-Signed-off-by: Dan Walsh <dwalsh@redhat.com> (github: rhatdan)

Dan Walsh authored on 2014/09/16 04:43:21
Showing 3 changed files
... ...
@@ -1044,6 +1044,7 @@ func (daemon *Daemon) checkLocaldns() error {
1044 1044
 	if err != nil {
1045 1045
 		return err
1046 1046
 	}
1047
+	resolvConf = utils.RemoveLocalDns(resolvConf)
1047 1048
 	if len(daemon.config.Dns) == 0 && utils.CheckLocalDns(resolvConf) {
1048 1049
 		log.Infof("Local (127.0.0.1) DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v", DefaultDns)
1049 1050
 		daemon.config.Dns = DefaultDns
... ...
@@ -27,3 +27,34 @@ func TestMergeLxcConfig(t *testing.T) {
27 27
 		t.Fatalf("expected %s got %s", expected, cpuset)
28 28
 	}
29 29
 }
30
+
31
+func TestRemoveLocalDns(t *testing.T) {
32
+	ns0 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\n"
33
+
34
+	if result := utils.RemoveLocalDns([]byte(ns0)); result != nil {
35
+		if ns0 != string(result) {
36
+			t.Fatalf("Failed No Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
37
+		}
38
+	}
39
+
40
+	ns1 := "nameserver 10.16.60.14\nnameserver 10.16.60.21\nnameserver 127.0.0.1\n"
41
+	if result := utils.RemoveLocalDns([]byte(ns1)); result != nil {
42
+		if ns0 != string(result) {
43
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
44
+		}
45
+	}
46
+
47
+	ns1 = "nameserver 10.16.60.14\nnameserver 127.0.0.1\nnameserver 10.16.60.21\n"
48
+	if result := utils.RemoveLocalDns([]byte(ns1)); result != nil {
49
+		if ns0 != string(result) {
50
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
51
+		}
52
+	}
53
+
54
+	ns1 = "nameserver 127.0.1.1\nnameserver 10.16.60.14\nnameserver 10.16.60.21\n"
55
+	if result := utils.RemoveLocalDns([]byte(ns1)); result != nil {
56
+		if ns0 != string(result) {
57
+			t.Fatalf("Failed Localhost: expected \n<%s> got \n<%s>", ns0, string(result))
58
+		}
59
+	}
60
+}
... ...
@@ -13,6 +13,7 @@ import (
13 13
 	"os"
14 14
 	"os/exec"
15 15
 	"path/filepath"
16
+	"regexp"
16 17
 	"runtime"
17 18
 	"strconv"
18 19
 	"strings"
... ...
@@ -332,6 +333,16 @@ func CheckLocalDns(resolvConf []byte) bool {
332 332
 	return true
333 333
 }
334 334
 
335
+var (
336
+	localHostRx = regexp.MustCompile(`(?m)^nameserver 127[^\n]+\n*`)
337
+)
338
+
339
+// RemoveLocalDns looks into the /etc/resolv.conf,
340
+// and removes any local nameserver entries.
341
+func RemoveLocalDns(resolvConf []byte) []byte {
342
+	return localHostRx.ReplaceAll(resolvConf, []byte{})
343
+}
344
+
335 345
 // GetLines parses input into lines and strips away comments.
336 346
 func GetLines(input []byte, commentMarker []byte) [][]byte {
337 347
 	lines := bytes.Split(input, []byte("\n"))