2

I have a spring application which is compileable/runs on vagrant and listens to localhost:8080 (inside vagrant).

This is my Vagrantfile:

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network :forwarded_port, guest: 80, host: 9000, host_ip: "127.0.0.1"
config.vm.provision :shell, path: "bootstrap.sh"
end

Now I want to access my spring-application from the host machine via localhost:9000.

Anyway the forwarded_port line doesn't work and I really have no idea why?

What do I have to change in my Vagrantfile?

SOLUTION:

With the Vagrantfile below it works for me.

Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provision :shell, path: "bootstrap.sh"

config.vm.provider "virtualbox" do |v|
        v.destroy_unused_network_interfaces = true
        v.memory = 2048
        v.cpus = 4
end
end
SteveOhio
  • 569
  • 2
  • 8
  • 20

2 Answers2

3

It doesn't work because the app is listening on localhost.

Make the app listen on a regular interface. You might also need to add guest_ip if it's not the Vagrant interface.

config.vm.network :forwarded_port, guest: 80, guest_ip: "xxx.xxx.xxx.xxx", host: 9000, host_ip: "127.0.0.1
techraf
  • 64,883
  • 27
  • 193
  • 198
0

If your spring app running in the VM is indeed listening to its local port 8080, and you want to access that app as http://localhost:9000/ from your host machine, then the line in your Vagrantfile should be:

config.vm.network :forwarded_port, guest: 8080, host: 9000

i.e. you have a typo specifying port 80 on the guest rather than 8080.

You probably don't need to specify any other parameters such as host_ip or guest_ip.

Dallas
  • 103
  • 1
  • 1
  • 8