When playing locally on a developer’s laptop, it’s handy or needed to modify your laptop’s hosts file to fake some DNS entries. That’s C:\Windows\System32\drivers\etc\hosts on Windows and /etc/hosts on Mac/Linux. By default, Docker Toolbox won’t see these custom DNS entries. Here’s how to change that.

In my Windows laptop, I have a custom hosts file which, among others, has the line: 192.168.99.100 teamcity.local. If I open a regular Windows command prompt, I can ping that address:

> ping teamcity.local

Pinging teamcity.local [192.168.99.100] with 32 bytes of data:
Reply from 192.168.99.100: bytes=32 time<1ms TTL=64
Reply from 192.168.99.100: bytes=32 time<1ms TTL=64

From within docker machine, I can’t:

ngeor@ENVY170124 MINGW64 ~
$ docker-machine ssh
Boot2Docker version 17.12.0-ce, build HEAD : 378b049 - Wed Dec 27 23:39:20 UTC 2017
Docker version 17.12.0-ce, build c97c6d6
docker@default:~$ ping teamcity.local
ping: bad address 'teamcity.local'

What about from within a container?

$ docker run -it ubuntu bash
# apt update
# apt install iputils-ping
# ping teamcity.local
ping: unknown host teamcity.local

To fix that, we need to modify the virtual machine that powers Docker Toolbox. According to the documentation, what we’re experiencing is the default behavior. We need to configure the VM to pass all DNS requests through the host, which will pick up the custom hosts file as well.

First, we need to shut down the virtual machine with docker-machine stop. Then, we modify Docker Toolbox’s VM, which by default is named “default”:

C:\Program Files\Oracle\VirtualBox>VBoxManage modifyvm "default" --natdnshostresolver1 on

We can start Docker Toolbox again and try to ping the custom host. First, from within docker machine:

$ docker-machine ssh
Boot2Docker version 17.12.0-ce, build HEAD : 378b049 - Wed Dec 27 23:39:20 UTC 2017
Docker version 17.12.0-ce, build c97c6d6
docker@default:~$ ping teamcity.local
PING teamcity.local (192.168.99.100): 56 data bytes
64 bytes from 192.168.99.100: seq=0 ttl=64 time=0.035 ms

and then we can also try from within a container:

$ docker run -it ubuntu bash
# apt update
# apt install iputils-ping
# ping teamcity.local
PING teamcity.local (192.168.99.100) 56(84) bytes of data.
64 bytes from teamcity.local (192.168.99.100): icmp_seq=1 ttl=64 time=0.028 ms
64 bytes from teamcity.local (192.168.99.100): icmp_seq=2 ttl=64 time=0.036 ms

Both cases worked fine. Now the custom hosts file on the laptop is taken into account.