Domanda

I'm wondering how to setup a Vagrant file that will put up a machine with two ports forwarded. This is my current Vagrantfile, which is forwarding the 8080 page:

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

Thanks!

È stato utile?

Soluzione

If you want to forward two ports, you may simply add another line like this:

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

A better way, in my opinion, is to setup a private network (or host-only network), so that you don't have to forward all the ports manually.

See my post here: Vagrant reverse port forwarding?

Additional tips

If you use the :id feature when defining :forward_port entries you need to make sure that each is unique. Otherwise they'll clobber each other, and the last one defined will typically win out.

For example:

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'

Altri suggerimenti

You can forward as many ports as you want (if those ports are not used by host machine), as following:

# 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

If you want to forward a range of ports, for loop also can be used like this:

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

If you are using chef kitchen, then the ports are set in the .kitchen.yml file as follows:

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

this will put the following lines in the .kitchen/kitchen-vagrant/Vagrantfile:

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

don't forget to do a kitchen destroy and kitchen create.

see:

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

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

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

I wasn't able to access my app without the host argument :

ng serve --host 0.0.0.0
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top