Search code examples
vagrantspring-cloud-consul

Vagrant forwarded_port on sub-machine


I couldn't manage to publish a virtual machine's port this way:

config.vm.define "n1" do |n1|
    n1.vm.hostname = "n1"
    n1.vm.network "private_network", ip: "172.20.20.10"
    n1.vm.network "forwarded_port", guest: 8500, host: 8080
end

Access inside the VM works fine:

vagrant@n1:~$ curl http://localhost:8500/v1/health/state/any

but host access (outside of the VM, from my computer web browser) won't work:

http://localhost:8080/v1/health/state/any

Is what I try to achieve possible? Can somebody give me an hint, please?


Solution

  • I am not vagrant expert. BUT, in networking, you CANNOT forward port that bind to localhost(127.0.0.1) without explicitly mentioned it. Universal binding only works for 0.0.0.0.

    You may try this, but I wouldn't guarantee it will works

    n1.vm.network "forwarded_port", guest: 8500, guest_ip: 127.0.0.1, host: 8080
    

    It is better to start your application binding to IP address then do the forwarding.