Define custom port_check method for halted docker containers

This commit is contained in:
Brian Cain 2020-05-12 11:17:15 -07:00
parent 5aff6660fb
commit c18ceb20bc
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 34 additions and 12 deletions

View File

@ -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,

View File

@ -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