diff --git a/plugins/guests/linux/cap/port.rb b/plugins/guests/linux/cap/port.rb new file mode 100644 index 000000000..62ffb5e7a --- /dev/null +++ b/plugins/guests/linux/cap/port.rb @@ -0,0 +1,11 @@ +module VagrantPlugins + module GuestLinux + module Cap + class Port + def self.port_open_check(machine, port) + machine.communicate.test("nc -z 127.0.0.1 #{port}") + end + end + end + end +end diff --git a/plugins/guests/linux/plugin.rb b/plugins/guests/linux/plugin.rb index 4b8810769..ab083d2be 100644 --- a/plugins/guests/linux/plugin.rb +++ b/plugins/guests/linux/plugin.rb @@ -51,6 +51,12 @@ module VagrantPlugins Cap::NFSClient end + # For the Docker provider + guest_capability("linux", "port_open_check") do + require_relative "cap/port" + Cap::Port + end + guest_capability("linux", "read_ip_address") do require_relative "cap/read_ip_address" Cap::ReadIPAddress diff --git a/plugins/providers/docker/action.rb b/plugins/providers/docker/action.rb index 2eee6dd0b..f95c015b9 100644 --- a/plugins/providers/docker/action.rb +++ b/plugins/providers/docker/action.rb @@ -15,7 +15,7 @@ module VagrantPlugins b.use Call, IsState, :not_created do |env, b2| # If the VM is NOT created yet, then do the setup steps if env[:result] - b2.use EnvSet, :port_collision_repair => true + b2.use EnvSet, port_collision_repair: true b2.use Call, HasSSH do |env2, b3| if env2[:result] @@ -27,14 +27,8 @@ module VagrantPlugins end end - b2.use Call, HostMachineRequired do |env2, b3| - if !env[:result] - # We're not using a proxy host VM, so just handle - # port collisions like we would any other system. - b3.use HandleForwardedPortCollisions - end - end - + b2.use HostMachinePortChecker + b2.use HandleForwardedPortCollisions b2.use PrepareNFSValidIds b2.use SyncedFolderCleanup b2.use SyncedFolders @@ -225,6 +219,7 @@ module VagrantPlugins autoload :Destroy, action_root.join("destroy") autoload :HasSSH, action_root.join("has_ssh") autoload :HostMachine, action_root.join("host_machine") + autoload :HostMachinePortChecker, action_root.join("host_machine_port_checker") autoload :HostMachineRequired, action_root.join("host_machine_required") autoload :HostMachineSyncFolders, action_root.join("host_machine_sync_folders") autoload :HostMachineSyncFoldersDisable, action_root.join("host_machine_sync_folders_disable") diff --git a/plugins/providers/docker/action/create.rb b/plugins/providers/docker/action/create.rb index b6e7fab9d..6f849814e 100644 --- a/plugins/providers/docker/action/create.rb +++ b/plugins/providers/docker/action/create.rb @@ -59,7 +59,7 @@ module VagrantPlugins def forwarded_ports mappings = {} @machine.config.vm.networks.each do |type, options| - if type == :forwarded_port && options[:id] != 'ssh' + if type == :forwarded_port mappings[options[:host]] = options end end diff --git a/plugins/providers/docker/action/host_machine_port_checker.rb b/plugins/providers/docker/action/host_machine_port_checker.rb new file mode 100644 index 000000000..949605ac6 --- /dev/null +++ b/plugins/providers/docker/action/host_machine_port_checker.rb @@ -0,0 +1,31 @@ +require "log4r" + +module VagrantPlugins + module DockerProvider + module Action + # This sets up the middleware env var to check for ports in use. + class HostMachinePortChecker + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::docker::hostmachineportchecker") + end + + def call(env) + return @app.call(env) if !env[:machine].provider.host_vm? + + @machine = env[:machine] + env[:port_collision_port_check] = method(:port_check) + + @app.call(env) + end + + protected + + def port_check(port) + host_machine = @machine.provider.host_vm + host_machine.guest.capability(:port_open_check, port) + end + end + end + end +end