Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)
| ... | ... |
@@ -920,6 +920,7 @@ func (container *Container) setupContainerDns() error {
|
| 920 | 920 |
} |
| 921 | 921 |
|
| 922 | 922 |
if config.NetworkMode != "host" {
|
| 923 |
+ // check configurations for any container/daemon dns settings |
|
| 923 | 924 |
if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
|
| 924 | 925 |
var ( |
| 925 | 926 |
dns = resolvconf.GetNameservers(resolvConf) |
| ... | ... |
@@ -936,15 +937,15 @@ func (container *Container) setupContainerDns() error {
|
| 936 | 936 |
dnsSearch = daemon.config.DnsSearch |
| 937 | 937 |
} |
| 938 | 938 |
return resolvconf.Build(container.ResolvConfPath, dns, dnsSearch) |
| 939 |
- } else {
|
|
| 940 |
- resolvConf = utils.RemoveLocalDns(resolvConf) |
|
| 941 |
- if !bytes.Contains(resolvConf, []byte("nameserver")) {
|
|
| 942 |
- for _, dns := range DefaultDns {
|
|
| 943 |
- log.Infof("No non localhost DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v", DefaultDns)
|
|
| 944 |
- resolvConf = append(append(resolvConf, []byte("\nnameserver ")...), dns...)
|
|
| 945 |
- } |
|
| 946 |
- resolvConf = append(resolvConf, []byte("\n")...)
|
|
| 947 |
- } |
|
| 939 |
+ } |
|
| 940 |
+ |
|
| 941 |
+ // replace any localhost/127.* nameservers |
|
| 942 |
+ resolvConf = utils.RemoveLocalDns(resolvConf) |
|
| 943 |
+ // if the resulting resolvConf is empty, use DefaultDns |
|
| 944 |
+ if !bytes.Contains(resolvConf, []byte("nameserver")) {
|
|
| 945 |
+ log.Infof("No non localhost DNS resolver found in resolv.conf and containers can't use it. Using default external servers : %v", DefaultDns)
|
|
| 946 |
+ // prefix the default dns options with nameserver |
|
| 947 |
+ resolvConf = append(resolvConf, []byte("\nnameserver "+strings.Join(DefaultDns, "\nnameserver "))...)
|
|
| 948 | 948 |
} |
| 949 | 949 |
} |
| 950 | 950 |
return ioutil.WriteFile(container.ResolvConfPath, resolvConf, 0644) |
| ... | ... |
@@ -1266,20 +1266,39 @@ func TestRunWithVolumesIsRecursive(t *testing.T) {
|
| 1266 | 1266 |
} |
| 1267 | 1267 |
|
| 1268 | 1268 |
func TestRunDnsDefaultOptions(t *testing.T) {
|
| 1269 |
+ // ci server has default resolv.conf |
|
| 1270 |
+ // so rewrite it for the test |
|
| 1271 |
+ origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
|
|
| 1272 |
+ if os.IsNotExist(err) {
|
|
| 1273 |
+ t.Fatalf("/etc/resolv.conf does not exist")
|
|
| 1274 |
+ } |
|
| 1275 |
+ |
|
| 1276 |
+ // test with file |
|
| 1277 |
+ tmpResolvConf := []byte("nameserver 127.0.0.1")
|
|
| 1278 |
+ if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
|
| 1279 |
+ t.Fatal(err) |
|
| 1280 |
+ } |
|
| 1281 |
+ // put the old resolvconf back |
|
| 1282 |
+ defer func() {
|
|
| 1283 |
+ if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
|
|
| 1284 |
+ t.Fatal(err) |
|
| 1285 |
+ } |
|
| 1286 |
+ }() |
|
| 1287 |
+ |
|
| 1269 | 1288 |
cmd := exec.Command(dockerBinary, "run", "busybox", "cat", "/etc/resolv.conf") |
| 1270 | 1289 |
|
| 1271 | 1290 |
actual, _, err := runCommandWithOutput(cmd) |
| 1272 | 1291 |
if err != nil {
|
| 1273 |
- t.Fatal(err, actual) |
|
| 1274 |
- } |
|
| 1275 |
- |
|
| 1276 |
- resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
|
|
| 1277 |
- if os.IsNotExist(err) {
|
|
| 1278 |
- t.Fatalf("/etc/resolv.conf does not exist")
|
|
| 1292 |
+ t.Error(err, actual) |
|
| 1293 |
+ return |
|
| 1279 | 1294 |
} |
| 1280 | 1295 |
|
| 1281 |
- if actual != string(resolvConf) {
|
|
| 1282 |
- t.Fatalf("expected resolv.conf is not the same of actual")
|
|
| 1296 |
+ // check that the actual defaults are there |
|
| 1297 |
+ // if we ever change the defaults from google dns, this will break |
|
| 1298 |
+ expected := "\nnameserver 8.8.8.8\nnameserver 8.8.4.4" |
|
| 1299 |
+ if actual != expected {
|
|
| 1300 |
+ t.Errorf("expected resolv.conf be: %q, but was: %q", expected, actual)
|
|
| 1301 |
+ return |
|
| 1283 | 1302 |
} |
| 1284 | 1303 |
|
| 1285 | 1304 |
deleteAllContainers() |