0

我想要做的是同时运行 2 个或更多 vagrant box。现在我遇到的问题是我收到警告“转发到 2222 的端口已在主机上使用”。这很奇怪,因为在 puPHPet 设置期间我没有将任何 Vagrant 框设置为端口 2222。

以下是我的 2 个 config.yaml 文件中的相关部分:

vagrantfile-local:
    vm:
        box: puphpet/ubuntu1404-x64
        box_url: puphpet/ubuntu1404-x64
        hostname: ''
        memory: '1024'
        cpus: '1'
        chosen_provider: virtualbox
        network:
            private_network: 192.168.56.101
            forwarded_port:
                dL5if56LAPQo:
                    host: '6645'
                    guest: '22'

---
vagrantfile-local:
    vm:
        box: puphpet/ubuntu1404-x64
        box_url: puphpet/ubuntu1404-x64
        hostname: ''
        memory: '1024'
        cpus: '1'
        chosen_provider: virtualbox
        network:
            private_network: 192.168.56.102
            forwarded_port:
                dL5if56LAPQo:
                    host: '6646'
                    guest: '22'

我猜这是我的错,但我没有看到在任何地方分配的端口 2222(可能是默认值)。

4

1 回答 1

0

It is a bit of a cheat and it doesn't solve the core problem, but it's good enough for me. What I did to solve the problem was change the Vagrant file that puPHPet generated. I changed the forwarded ports line to:

data['vm']['network']['forwarded_port'].each do |i, port|
    if port['guest'] != '' && port['host'] != ''
      config.vm.network :forwarded_port, guest: port['guest'].to_i, host: port['host'].to_i, auto_correct: true
    end
  end

I added ", auto_correct: true". This is what the Vagrant documentation says about auto_correct:


PORT COLLISIONS AND CORRECTION

It is common when running multiple Vagrant machines to unknowingly create forwarded port definitions that collide with each other (two separate Vagrant projects forwarded to port 8080, for example). Vagrant includes built-in mechanism to detect this and correct it, automatically.

Port collision detection is always done. Vagrant will not allow you to define a forwarded port where the port on the host appears to be accepting traffic or connections.

Port collision auto-correction must be manually enabled for each forwarded port, since it is often surprising when it occurs and can lead the Vagrant user to think that the port wasn't properly forwarded. Enabling auto correct is easy:

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

The final :auto_correct parameter set to true tells Vagrant to auto correct any collisions. During a vagrant up or vagrant reload, Vagrant will output information about any collisions detections and auto corrections made, so you can take notice and act accordingly.


I couldn't have explained it better :)

于 2015-03-20T09:13:54.113 回答