我有几个流浪的虚拟机(在Mac和Windows上,都运行Linux(。我在Vagrant文件中定义了这样的端口转发:
config.vm.network "forwarded_port", guest: 80, host: 9007
我第一次做vagrant up
它工作正常。但是当我做一个vagrant reload
或vagrant halt
然后是vagrant up
时,我会收到以下消息:
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 9007 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: 80, 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.
然后我必须更改端口才能启动VM,这很痛苦。我已经检查了netstat和lsof,似乎没有任何东西使用该端口。我虽然这可能是一个时间问题,一段时间后我可以使用该端口,但即使等待了几个小时,我仍然会收到错误。
这在Mac和Windows环境中都会发生。是否有一些设置可以让我重复使用端口?
我通常使用静态 IP 来避免端口转发问题。Vagrant有一个auto_correct
功能来帮助解决这个问题。
从流浪者转发端口文档
运行多台流浪者机器时,在不知不觉中 创建相互冲突的转发端口定义(两个 例如,转发到端口 8080 的单独流浪者项目(。 Vagrant包括内置机制来检测并纠正它, 自然而然。
端口冲突检测始终完成。流浪汉不会允许你 定义主机上的端口显示为 接受流量或连接。
必须为每个手动启用端口冲突自动更正 转发端口,因为当它发生时通常会令人惊讶并且可以 导致流浪者用户认为端口不正确 转发。启用自动更正很容易:
Vagrant.configure("2") do |config| config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true end
最终
:auto_correct
参数设置为 true 告诉流浪者自动 更正任何冲突。在流浪者向上或流浪者重新加载期间,流浪者 将输出有关任何碰撞检测和自动的信息 进行了更正,以便您可以注意并采取相应行动。