Outbound connections from Docker with VirtualBox not working?

Using Docker and VirtualBox and having trouble making outbound connections? You may have errors similar to:

  • curl: (7) Failed connect to; No route to host
  • Error: Redis connection to redis:6379 failed - connect EHOSTUNREACH

Here’s what I did to fix it:

  • I ssh’d into the Docker machine:

    $ docker-machine ssh dev
  • Checked eth0’s subnet:

    $ ifconfig eth0
    eth0      Link encap:Ethernet  HWaddr 08:00:27:C8:C7:50
    inet addr:  Bcast:  Mask:
    inet6 addr: fe80::a00:27ff:fec8:c750/64 Scope:Link
    RX packets:230796 errors:0 dropped:0 overruns:0 frame:0
    TX packets:62400 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:238029328 (227.0 MiB)  TX bytes:4768029 (4.5 MiB)
  • Modified the NAT’s subnet to match:

    $ docker-machine stop dev
    $ VBoxManage modifyvm dev --natnet1 ""
    $ docker-machine restart dev

And just like that, it works:

$ docker@dev:~$ curl
HTTP/1.1 200 OK