Portforwarding for multiple VMs using same port fo

2019-08-07 16:19发布

问题:

I currently have a vagrant environment where each VM has a port forwarding rule setup from host to guest. However the problem with this forwarding rule is that I cannot have multiple VMs running via vagrant at the same time with similar port forwarding rules.

It seems like I'll need some sort of a proxy for ports to handle this. Is there something that is already baked into vagrant/virtualbox that could support this?

Traceback from Vagrant when I try running another VM with the same forwarding rules:
Vagrant cannot forward the specified ports on this VM, since they
would collide with some other application that is already listening
on these ports. The forwarded port to 8000 is already in use
on the host machine.

To fix this, modify your current projects Vagrantfile to use another
port. Example, where '1234' would be replaced by a unique host port:

 config.vm.network :forwarded_port, guest: 8000, host: 1234

Sometimes, Vagrant will attempt to auto-correct this for you. In this
case, Vagrant was unable to. This is usually because the guest machine
is in a state which doesn't allow modifying port forwarding.

回答1:

You can use the :auto_correct parameter for the port definition, then Vagrant will look for another (non-used) port:

Vagrant.configure("2") do |config|
  config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
end

See the documentation for more details.