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!
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.
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.
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.
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?
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With