From c18ceb20bc0ddeec67a2fcfab718758f55413006 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 12 May 2020 11:17:15 -0700 Subject: [PATCH] Define custom port_check method for halted docker containers --- .../handle_forwarded_port_collisions.rb | 10 ------ ...prepare_forwarded_port_collision_params.rb | 36 +++++++++++++++++-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb index 61acdf914..917cbde8d 100644 --- a/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb +++ b/lib/vagrant/action/builtin/handle_forwarded_port_collisions.rb @@ -124,16 +124,6 @@ module Vagrant call_port_checker(port_checker, host_ip, host_port) || lease_check(host_ip, host_port) - # This check is required only for the docker provider. Containers - # can bind ports but be halted. We don't want new containers to - # grab these bound ports, so this check is here for that since - # the checks above won't detect it - if !in_use && env[:machine].provider_name == :docker - if extra_in_use.keys.include?(host_port.to_s) - in_use = true - end - end - if in_use if !repair || !options[:auto_correct] raise Errors::ForwardPortCollision, diff --git a/plugins/providers/docker/action/prepare_forwarded_port_collision_params.rb b/plugins/providers/docker/action/prepare_forwarded_port_collision_params.rb index cf4548378..2a838a51b 100644 --- a/plugins/providers/docker/action/prepare_forwarded_port_collision_params.rb +++ b/plugins/providers/docker/action/prepare_forwarded_port_collision_params.rb @@ -15,14 +15,46 @@ module VagrantPlugins env[:port_collision_extra_in_use] = other_used_ports # Build the remap for any existing collision detections + # + # Note: This remap might not be required yet (as it is with the virtualbox provider) + # so for now we leave the remap hash empty. remap = {} env[:port_collision_remap] = remap - # Note: This might not be required yet (as it is with the virtualbox provider) - # so for now we leave the remap hash empty. + # This port checker method calls the custom port_check method + # defined below. If its false, it will go ahead and use the built-in + # port_check method to see if there are any live containers with bound + # ports + docker_port_check = proc { |host_ip, host_port| + result = port_check(env, host_port) + if !result + result = Vagrant::Action::Builtin::HandleForwardedPortCollisions.port_check(machine, host_ip, host_port) + end + result} + env[:port_collision_port_check] = docker_port_check @app.call(env) end + + protected + + # This check is required the docker provider. Containers + # can bind ports but be halted. We don't want new containers to + # grab these bound ports, so this check is here for that since + # the checks above won't detect it + # + # @param [Vagrant::Environment] env + # @param [String] host_port + # @returns [Bool] + def port_check(env, host_port) + extra_in_use = env[:port_collision_extra_in_use] + + if extra_in_use + return extra_in_use.include?(host_port.to_s) + else + return false + end + end end end end