Class: Minke::Docker::ServiceDiscovery

Inherits:
Object
  • Object
show all
Defined in:
lib/minke/docker/service_discovery.rb

Overview

ServiceDiscovery allows you to look up the publicly accessible address and port for a server

Instance Method Summary collapse

Constructor Details

#initialize(project_name, docker_runner) ⇒ ServiceDiscovery

Returns a new instance of ServiceDiscovery.



6
7
8
9
# File 'lib/minke/docker/service_discovery.rb', line 6

def initialize project_name, docker_runner
  @project_name = project_name
  @docker_runner = docker_runner
end

Instance Method Details

#bridge_address_for(network, service_name, private_port) ⇒ Object

Will attempt to locate the private details for a running container given  its name and private port Parameters:

  • network: the name of the network the container is running on

  • service_name: the name of the running service

  • private_port: the private port which you wish to retrieve an address for

Returns:  private address for the container e.g. 172.17.0.2:8080



40
41
42
43
44
45
46
47
48
# File 'lib/minke/docker/service_discovery.rb', line 40

def bridge_address_for network, service_name, private_port
  begin
    container_details = find_container_by_name "/#{@project_name}_#{service_name}_1"
    ip = container_details.first.info['NetworkSettings']['Networks']["#{network}"]['IPAddress']
  rescue
    raise "Unable to find bridge address for network: #{network}, container: #{service_name}, port: #{private_port}"
  end
  return "#{ip}:#{private_port}"
end

#find_container_by_name(name) ⇒ Object



51
52
53
54
# File 'lib/minke/docker/service_discovery.rb', line 51

def find_container_by_name name
  containers = @docker_runner.running_containers
  containers.select { |c| c.info['Names'].include?(name) }
end

#public_address_for(service_name, private_port) ⇒ Object

Will attempt to locate the public details for a running container given  its name and private port Parameters:

  • service_name: the name of the running service

  • private_port: the private port which you wish to retrieve an address for

Returns:  public address for the container e.g. 0.0.0.0:8080



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/minke/docker/service_discovery.rb', line 19

def public_address_for service_name, private_port
  begin
    ip = @docker_runner.get_docker_ip_address
    container_details = find_container_by_name "/#{@project_name}_#{service_name}_1"
    ports = container_details.first.info['Ports'].select { |p| p['PrivatePort'] == private_port }.first
  rescue
    raise "Unable to find public address for '#{service_name}' on port #{private_port}"
  end

  return "#{ip}:#{ports['PublicPort']}"
end