Method: Docker::Compose::NetInfo#host_routable_ip

Defined in:
lib/docker/compose/net_info.rb

#host_routable_ip(target_ip = docker_routable_ip) ⇒ String

Examine local host’s network interfaces; figure out which one is most likely to share a route with the given IP address. If no IP address is specified, figure out which IP the Docker daemon is reachable on and use that as the target IP.

Parameters:

  • target_ip (String) (defaults to: docker_routable_ip)

    IPv4 address of target

Returns:

  • (String)

    IPv4 address of host machine that may be reachable from Docker machine

[View source]

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/docker/compose/net_info.rb', line 43

def host_routable_ip(target_ip = docker_routable_ip)
  best_match  = ''
  best_prefix = 0

  target_cps = target_ip.codepoints

  @my_ips.each do |my_ip|
    ip_cps = my_ip.codepoints
    prefix = 0
    ip_cps.each_with_index do |cp, i|
      break unless target_cps[i] == cp
      prefix = i
    end

    if prefix > best_prefix
      best_match = my_ip
      best_prefix = prefix
    end
  end

  best_match
end