在重新加载后,vagrant ports报告正在使用中

时间:2017-08-24 00:22:07

标签: vagrant vagrantfile vagrant-windows

我有一些流浪的虚拟机(在Mac和Windows上都运行Linux)。我在Vagrant文​​件中定义了这样的端口转发:

  config.vm.network "forwarded_port", guest: 80, host: 9007

我第一次做vagrant up时效果很好。但是,当我执行vagrant reloadvagrant 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环境中都会发生这种情况。是否有一些设置可以让我重用端口?

1 个答案:

答案 0 :(得分:0)

我通常使用静态IP来避免端口转发问题。 Vagrant有一个auto_correct功能来帮助解决这个问题。

来自vagrant forwarded port docs

  

在不知不觉中运行多个Vagrant机器时很常见   创建相互冲突的转发端口定义(两个   单独的Vagrant项目转发到端口8080,例如)。   Vagrant包含内置机制来检测并纠正它,   自动。

     

始终进行端口碰撞检测。 Vagrant不允许你这样做   定义主机上的端口所在的转发端口   接受交通或连接。

     

必须为每个人手动启用端口冲突自动更正   转发端口,因为它发生时通常会令人惊讶并且可以   引导Vagrant用户认为端口不正确   转发。启用自动更正非常简单:

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

设置为true的最终:auto_correct参数告知Vagrant为auto   纠正任何碰撞。在流浪汉或流浪汉重新加载期间,Vagrant   将输出有关任何碰撞检测和自动的信息   更正了,所以你可以注意并采取相应的行动。