diff --git a/lib/vagrant/action/vm/check_port_collisions.rb b/lib/vagrant/action/vm/check_port_collisions.rb index ae83492b5..22c2e7eec 100644 --- a/lib/vagrant/action/vm/check_port_collisions.rb +++ b/lib/vagrant/action/vm/check_port_collisions.rb @@ -15,9 +15,21 @@ module Vagrant # Figure out how we handle port collisions. By default we error. handler = env[:port_collision_handler] || :error + # Read our forwarded ports, if we have any, to override what + # we have configured. + current = {} + env[:vm].driver.read_forwarded_ports.each do |nic, name, hostport, guestport| + current[name] = hostport.to_i + end + existing = env[:vm].driver.read_used_ports env[:vm].config.vm.forwarded_ports.each do |name, options| - if existing.include?(options[:hostport].to_i) + # Use the proper port, whether that be the configured port or the + # port that is currently on use of the VM. + hostport = options[:hostport].to_i + hostport = current[name] if current.has_key?(name) + + if existing.include?(hostport) # We have a collision! Handle it send("handle_#{handler}".to_sym, name, options, existing) end diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index ed160a52e..fc5a3f3d5 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -35,7 +35,7 @@ module Vagrant :auto => false }.merge(options || {}) - forwarded_ports[name] = options + forwarded_ports[name.to_s] = options end def share_folder(name, guestpath, hostpath, opts=nil)