How to forward a port to vagrant guest if this port already listened by host machine

做~自己de王妃 提交于 2020-01-17 07:02:18

问题


For example I have an app which run vagrant machines dynamically and expect some info from them will be sent through http to specific host machine port. So, my app listening specified port (http server) and I can't forward that port:

C:\node-vagrant-test-task>vagrant reload ==> default: Clearing any previously set forwarded ports... 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 8080 is already in use on the host machine.

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

config.vm.network :forwarded_port, guest: 8080, 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. You could try 'vagrant reload' (equivalent of running a halt followed by an up) so vagrant can attempt to auto-correct this upon booting. Be warned that any unsaved work might be lost.


回答1:


If you do not want to mess with forwarding port, the best is to use a static IP for the guest

you can do either private or public networks with a static IP

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.50.4"
end

or

Vagrant.configure("2") do |config|
  config.vm.network "public_network", ip: "192.168.50.4"
end

so you dont need to forward the 8080 port and can access your app directly on http://192.168.50.4:8080

If you need to then access the host machine for this guest, you can access it via 192.168.50.1



来源:https://stackoverflow.com/questions/44108551/how-to-forward-a-port-to-vagrant-guest-if-this-port-already-listened-by-host-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!