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

我想知道如何设置一个Vagrant文​​件,将build立一个机器与两个端口转发。 这是我目前正在转发8080页面的Vagrantfile:

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 

谢谢!

如果你想转发两个端口,你可以简单的添加另外一行:

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

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

在这里看到我的post: stream浪汉反向端口转发?

其他提示

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

例如:

 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' 

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

 # 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 

如果您正在使用厨师厨房,那么端口将被设置在.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