Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Vagrant forward multiple ports on the same machine?

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!

like image 201
fegemo Avatar asked Jul 12 '13 20:07

fegemo


People also ask

What is port forwarding Vagrant?

Vagrant forwarded ports allow you to access a port on your host machine and have all data forwarded to a port on the guest machine, over either TCP or UDP.

Which interface should the network bridge to Vagrant?

Default Network Interface If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.

How do I access my host on Vagrant?

You can reach your host from your guest by using the default gateway on your VM. See this answer for an explanation. By running netstat -rn you can get the default gateway and then use that ip address in your config file of your application.


1 Answers

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' 
like image 69
Mingyu Avatar answered Sep 25 '22 01:09

Mingyu