-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11602 from briancain/feature/docker-port-collisio…
…n-fix Fixes #9067: Ensure new containers don't grab existing bound ports
- Loading branch information
Showing
12 changed files
with
222 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Vagrant | ||
module Util | ||
module IPv4Interfaces | ||
def ipv4_interfaces | ||
Socket.getifaddrs.select do |ifaddr| | ||
ifaddr.addr && ifaddr.addr.ipv4? | ||
end.map do |ifaddr| | ||
[ifaddr.name, ifaddr.addr.ip_address] | ||
end | ||
end | ||
|
||
extend IPv4Interfaces | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
plugins/providers/docker/action/prepare_forwarded_port_collision_params.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module VagrantPlugins | ||
module DockerProvider | ||
module Action | ||
class PrepareForwardedPortCollisionParams | ||
def initialize(app, env) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
machine = env[:machine] | ||
|
||
# Get the forwarded ports used by other containers and | ||
# consider those in use as well. | ||
other_used_ports = machine.provider.driver.read_used_ports | ||
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 | ||
|
||
# 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 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.