This post is to show how to fix “An error occurred trying to connect: …” error after installing a fresh Docker on Windows:
So, asuming, you have downloaded the latest Docker for Windows package from this link – https://docs.docker.com/installation/windows/. You install VirtualBox and other components. You open a VirtualBox manager to see that there is nothing yet created :), then you double click on “Boot2Docker start” icon and you see how a new virtual machine is being created and started:
Then you run a “docker version” command to make sure everything is normal and you get that connection error.
If you look inside the Linux VM [ifconfig] you will see that it is configured with the same IP address as the docker client is trying to knock in, so should be fine on that side. Lets look at how the overall VirtualBox virtual networking is configured, go to File -> Preferences -> Network:
Lets look at the host [my laptop] routing configuration. Does it know where to send packets for the 192.168.59.x subnet? Run “route print”:
As you can see the route does exist. All the packets for the 192.168.59.0/24 destinations will be sent to the 192.168.59.3 interface [VirtualBox interface]. .3 is a lovely choise for a gateway isn’t it 🙂
OK, what about the VM network settings? There are two network adapters configured. First one is set as NAT [we know that NAT is not configured]:
Hey, it works 🙂 But wait a minute. How did the Linux machine gets the IP address? It is not static, it’s been given via DHCP. VirtualBox NIC #3 is configured with the DHCP scope. So if you reboot the VM it’s not going to get an IP address and we suck again. To fix that issue we need to create a DHCP scope on #2:
Now if you reboot, it should work.
If you wonder how Docker Client knows that the IP address of a VM is 192.168.59.103, then you are with me 🙂 The only thing I was able to figure out is that it’s via “boot2docker.exe ip” command (read the comments below for more):
OK, finally we can try running something inside the Docker. There is a tiny image called “hello-world” in the repository online. So if you type “docker run hello-world” you should download it and see the following output: