如果主机已侦听此端口,如何将端口转发给 vagrant guest

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

例如,我有一个 运行 动态流浪机器的应用程序,并期望它们的一些信息将通过 http 发送到特定主机端口。所以,我的应用监听指定的端口(http 服务器),但我无法转发该端口:

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.

如果不想弄乱转发端口,最好是给guest使用静态IP

您可以使用静态 IP

建立私人网络或 public 网络
Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.50.4"
end

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

因此您无需转发 8080 端口,可以直接在 http://192.168.50.4:8080

上访问您的应用

如果您随后需要访问此访客的主机,您可以通过192.168.50.1

访问它