How can Vagrant forward multiple ports on the same machine?
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
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 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'