What is the simplest way to get vagrant/virtualbox to access host services?

2.5k Views Asked by At

I've been reading through many examples (both here and through various blogs and virtualbox/vagrant documentation) and at this point I think I should be able to do this.

What I ultimately would like to do is communicate with my docker daemon on my host machine and all the subsequent services I spin up arbitrarily.

To try to get this to work, I run the simple nginx container on my host and confirm it works:

$ docker run --name some-nginx -d -p 8080:80 docker.io/library/nginx:1.17.9
$ curl localhost:8080
> Welcome to nginx!

In my Vagrantfile I've define my host-only network:

config.vm.network "private_network", ip: "192.168.50.4",
  virtualbox__intnet: true

Now in my guest vagrant box, I expect that I should be able to access this same port:

$ curl localhost:8080
> curl: (7) Failed to connect to localhost port 8080: Connection refused

$ curl 127.0.0.1:8080
> curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused

$ curl 192.168.50.4:8080 # I hope not, but maybe this will work?
> curl: (7) Failed to connect to 192.168.50.4 port 8080: Connection refused
2

There are 2 best solutions below

0
On

If you're "inside" the Vagrant guest machine, localhost will be the local loopback adapter of THAT machine and not of your host.

In VirtualBox virtualization, which you are using, you can always connect to services running on your hosts' localhost via the 10.0.2.2 address. See: https://www.virtualbox.org/manual/ch06.html#network_nat

So in your case, with the web server running on port 8080 on your host, using

curl 10.0.2.2:8080

would mean success!

0
On
  • run vagrant up to start the vm and use NAT as network interface, which means guest vm will run as equal as the host in the same network.

  • vagrant ssh into the vm and install net-tools if you machine doesn't have tool netstat

  • use netstat -rn to find any routable gateway. Below gateways 10.0.2.2, 192.168.3.1 they're the gateways present in guest vm.

    [vagrant@localhost ~]$ netstat -rn
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
    0.0.0.0         10.0.2.2        0.0.0.0         UG        0 0          0 eth0
    0.0.0.0         192.168.3.1     0.0.0.0         UG        0 0          0 eth2
    10.0.2.0        0.0.0.0         255.255.255.0   U         0 0          0 eth0
    192.168.3.0     0.0.0.0         255.255.255.0   U         0 0          0 eth2
    192.168.33.0    0.0.0.0         255.255.255.0   U         0 0          0 eth1
    
  • Go to host and run ifconfig. Find out the gateway 192.168.3.1 shared at host and host possesses IP 192.168.3.x. And make sure service at host can be accessed at 192.168.3.x.

  • And make sure service at host can be accessed at 192.168.3.x.

    • try it at host curl -v http://<192.168.3.x>:<same port on host>, if can accessed, ok.
  • Now go to guest vm, try curl -v http://<192.168.3.x>:<same port on host>. If can be accessed too

  • now you can access services at host from guest vm.