Avoiding port collision running several Vagrant machines
Well, I’m not sure why this setting wasn’t the default but Vagrant actually allows you to tell it to find another port if the one it wants to forward is being used. It’s also dead simple and works like a charm. I only wish I could configure where it starts looking for ports.
Just add an extra parameter to the definition of your port forward:
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80, host: 8080,
Now whenever there’s a collision with a running machine you can effortlessly run other Vagrant instances. You’ll need to look at the output though to determine what the new forwards are.
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: A newer version of the box 'ubuntu/trusty64' is available! You currently
==> default: have version '20151117.0.0'. The latest is version '20151217.0.0'. Run
==> default: `vagrant box update` to update.
==> default: Fixed port collision for 8000 => 8080. Now on port 2200.
==> default: Fixed port collision for 9000 => 9000. Now on port 2201.
==> default: Fixed port collision for 22 => 2222. Now on port 2202.
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 8000 => 2200 (adapter 1)
default: 9000 => 2201 (adapter 1)
default: 22 => 2202 (adapter 1)
You’ll want to pay attention to the new forwarding information though. Apparently there’s no way to get a list of forward ports after you’ve brought up the machine using Vagrant. You can open up Virtual Box though, find the VM, open up Setting and go to the Network tab.
Once you figure out which adapter it’s using, click the Port Forwarding button at the bottom. That’ll show you which ports are forwarded.
Well, hope this helps.