Skip to content

Instantly share code, notes, and snippets.

@fredxia
Last active October 14, 2023 16:40
Show Gist options
  • Save fredxia/56d8bf02fdd3202e0eb22939f28a287a to your computer and use it in GitHub Desktop.
Save fredxia/56d8bf02fdd3202e0eb22939f28a287a to your computer and use it in GitHub Desktop.
WSL Ubuntu DNS Not Working with docker

Environment

WSL (version 2) with Ubuntu 20.04

Symptom

DNS stopped working after starting docker service

How to Fix

Configure dockerd to use a different subnet for its network interface. No need to disable auto-generation of /etc/resolv.conf in WSL.

Detail

When I run Ubuntu 20.04 in WSL DNS stopped working after systemctl start docker. Even if I subsequently stop docker service DNS still won't work. nslookup hangs.

There are some suggestions online to fix the problem. However none of them worked for me. For example, one suggestion is to disable the auto-generation of /etc/resolv.conf by creating a /etc/wsl.conf file, containing this configuration:

[network]
generateResolvConf = false

and then manually create /etc/resolve.conf with a hard-coded nameserver, e.g.:

nameserver 8.8.8.8

But this does not work for me even after shutdown/restart. DNS still stops working as soon as dockerd is started.

Noticing that by default dockerd creates a network interface on 172.x.x.x, e.g. 172.17.0.1, which is also the nameserver IP address that WSL auto-generates for /etc/resolv.conf, I tried to change docker daemon config so that it uses a different subnet, because WSL may internally still use 172.x.x.x to forward DNS request/response in the backend even if nameserver is hard-coded in /etc/resolv.conf.

So I added a file /etc/docker/daemon.js as:

{
    "bip" : "10.10.0.1/16"
}

After this change, and a restart of WSL virtual machine, DNS works fine after dockerd starts. There is no need to disable auto-generation of /etc/resolv.conf.

Now DNS inside a docker container still does not work. For example a pod in a minikube won't be able to resolve a DNS request. But this seems to be a separate issue, and can be addressed by explicitly setting the nameserver to some well-known IP address, e.g. 8.8.8.8, inside the running container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment