Vagrant如何在同一台机器上转发多个端口?

时间:2013-07-12 20:30:33

标签: vagrant portforwarding

我想知道如何设置一个Vagrant文​​件,该文件会放置一台带有两个端口转发的机器。 这是我目前的Vagrantfile,它正在转发8080页面:

Vagrant.configure("2") do |config|

  config.vm.box = "precise32"
  config.vm.box_url = "http://files.vagrantup.com/precise32.box"
  config.vm.provider "virtualbox"

  config.vm.network :forwarded_port, guest: 8080, host: 8080
  config.vm.provision :shell, :path => "start.sh", :args => "'/vagrant'"

  config.vm.network :public_network
end

谢谢!

4 个答案:

答案 0 :(得分:88)

如果要转发两个端口,可以添加另一行,如下所示:

config.vm.network :forwarded_port, guest: 8080, host: 8080
config.vm.network :forwarded_port, guest: 5432, host: 5432

在我看来,更好的方法是设置专用网络(或仅限主机的网络),这样您就不必手动转发所有端口。

在这里查看我的帖子: Vagrant reverse port forwarding?

其他提示

如果在定义:id条目时使用:forward_port功能,则需要确保每个条目都是唯一的。否则他们会互相辱骂,最后定义的通常会胜出。

例如:

config.vm.network "forwarded_port", guest: 8080, host: 8080, id: 'was_appserver_http'
config.vm.network "forwarded_port", guest: 9043, host: 9043, id: 'ibm_console_http'
config.vm.network "forwarded_port", guest: 9060, host: 9060, id: 'ibm_console_https'

答案 1 :(得分:9)

您可以根据需要转发任意数量的端口(如果这些端口未被主机使用),如下所示:

# for Redis
config.vm.network "forwarded_port", guest: 6379, host: 6379
# for HTTP
config.vm.network "forwarded_port", guest: 80, host: 80
# for MySQL
config.vm.network "forwarded_port", guest: 3306, host: 3306

如果您要转发范围的端口,for loop也可以像这样使用:

for i in 81..89
          config.vm.network :forwarded_port, guest: i, host: i
end
for i in 8080..8089
          config.vm.network :forwarded_port, guest: i, host: i
end

答案 2 :(得分:1)

如果您使用厨师厨房,则端口在.kitchen.yml文件中设置如下:

---
driver:
  name: vagrant
  network:
    - ["forwarded_port", {guest: 80, host: 40080}]
    - ["forwarded_port", {guest: 443, host: 40443}]
provisioner:
...

这会将以下行放在.kitchen / kitchen-vagrant / Vagrantfile中:

c.vm.network(:forwarded_port, {:guest=>80, :host=>40080})
c.vm.network(:forwarded_port, {:guest=>443, :host=>40443})

不要忘记做厨房破坏和厨房创造。

请参阅:

http://www.jeeatwork.com/?p=76

https://github.com/test-kitchen/kitchen-vagrant#-network

https://docs.chef.io/config_yml_kitchen.html

答案 3 :(得分:0)

如果没有host参数,我将无法访问我的应用程序:

ng serve --host 0.0.0.0