58

我想知道如何设置一个 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

4 回答 4

93

如果您想转发两个端口,您可以简单地添加另一行,如下所示:

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

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

在这里查看我的帖子: Vagrant 反向端口转发?

其他提示

如果您: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'
于 2013-07-12T21:19:42.313 回答
10

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

# 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
于 2016-05-24T13:53:20.703 回答
1

如果您使用的是 chef kitchen,则端口在 .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

于 2017-03-05T23:00:16.213 回答
0

没有主机参数,我无法访问我的应用程序:

ng serve --host 0.0.0.0
于 2019-07-21T16:19:00.820 回答