29

I know how to create a new docker-machine with dns settings

docker-machine create -d virtualbox --engine-opt dns=8.8.8.8 my_machine

But there is already a "default" virtualmachine so is there a way to change its dns?

I read online ways to do this with boot2docker, but that tool is deprecated and docker-machine has replaced it. Unfortunately, it's so new that I haven't found much online about this.

kane
  • 5,465
  • 6
  • 44
  • 72
  • My workaround is to remove the original "default" host and create a new one (same name) with the dns settings I need – kane Dec 15 '15 at 21:13

2 Answers2

43

Go to ~/.docker/machine/machines/default/config.json and add your own DNS server into HostOptions/EngineOptions/Dns and restart docker machine.

{  
   "HostOptions": {
        "Driver": "",
        "Memory": 0,
        "Disk": 0,
        "EngineOptions": {
            "ArbitraryFlags": [],
            "Dns": ["192.168.99.1","8.8.8.8","8.8.4.4"], <-- set it here
            "GraphDir": ""
        }
}

Edit:

The Dns setting in config.json seems to be ignored in the new version of docker-machine. The only thing that seems to work is to add line (edit this to match your needs)

"${DOCKER_MACHINE}" ssh "${VM}" "sudo sed -i.bkp '/--label provider=virtualbox/a --dns 8.8.8.8\\\n--dns 8.8.4.4' /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart"

after

yes | "${DOCKER_MACHINE}" regenerate-certs "${VM}"

in script start.sh, which is run every time Docker Quickstart Terminal is started.

Then shut down the machine (if it's running) and open a new instance of the Docker Quickstart Terminal.

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
Nat
  • 3,587
  • 20
  • 22
  • I saw the config and thought this might be an option. But it wasn't referenced anywhere so I wasn't sure if this was the standard method encouraged by Docker...? – kane Dec 15 '15 at 23:52
  • it might help others if you showed the contents of this config.json and a before/after snapshot of the settings for those who aren't familiar. I can accept this answer after that if a better one doesn't come up – kane Dec 16 '15 at 00:03
  • docker-machine is quite under documented. so config.json format is not publicly documented yet – Nat Dec 16 '15 at 01:16
  • 1
    In my `config.json` the "Dns" value was `null`. Replacing it with a list containing just the 8.8.x.x servers worked for me. I'm guessing the 192.168.99.1 is only needed if you have custom DNS entries set up locally. – amacleod Jun 09 '16 at 15:47
  • In my `config.json` the "Dns" value is also `null`, however at the runtime the machine /etc/resolv.conf has `192.168.1.1` set for name server. I am trying to understand where this IP came from. Note that this IP does not work for me. More strange thing is that, the docker-machine and the containers used to able to reach out the outside world, i.e. the DNS used to work, but I don't know what the value was when it was working. – user1783732 Nov 23 '16 at 00:34
  • 3
    FYI -- You can also `docker-machine provision name` to apply any config changes without needing to restart the machine! – nathanleclaire Jan 27 '17 at 19:32
  • 1
    The DNS setting does not modify `/etc/resolv.conf` in the VM. That is tied up in VirtualBox networking. It modifies the DNS server that the Docker daemon itself uses. – nathanleclaire Jan 27 '17 at 19:35
  • 2
    Set this config to `["8.8.8.8", "8.8.4.4"]`, restarted docker machine, restarted docker service, then restarted docker machine, then reprovisioned it, then restarted it again for good measure. The machine is inexplicably trying to use `10.0.2.3` as DNS server. – Caleb Fenton Sep 25 '17 at 21:38
  • 1
    This didn't solve it for me. I put the DNS server 8.8.8.8 there and I'm still getting the same error. – harrisonfooord Aug 21 '18 at 09:25
  • This didn't solve it for me too. I put the DNS server in `/etc/resolve.conf` file inside the machine!!! dns addresses inside `json.config` don't apply on virtual machine(`/etc/resolve.conf`) – msoa Aug 13 '19 at 03:22
  • anyone has solution to this? I recently ran into the same problem, and changing the Dns settings in config.json does not fix it. – TonyW Jan 31 '20 at 07:31
  • The only that seems to work is to add line "${DOCKER_MACHINE}" ssh "${VM}" "sudo sed -i.bkp '/--label provider=virtualbox/a --dns 8.8.8.8\\\n--dns 8.8.4.4' /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart" after yes | "${DOCKER_MACHINE}" regenerate-certs "${VM}" in script start.sh that is run every time Docker Quickstart Terminal is started. – Konrad Botor Jun 21 '21 at 10:32
5

On Windows 10 docker's gui give some facilities.

Just right click on docker's icon in the tray bar and select "Settings" item.

Docker's options

Then, on the Docker's window, select the "Network" section and change the DNS option from "Automatic" to "Fixed" and hit "Apply". Docker will restart itself after that. I putted the Google's DNS (8.8.8.8) and it worked fine to me.

docker's network section with dns config

Hope it helps.

mukade
  • 650
  • 1
  • 9
  • 11
  • When I change this option to fixed and set an ip then check the resolv.conf in the container... it's unchanged. I don't believe this works. – Brandon.Blanchard Jul 16 '18 at 23:57