Method: Docker::Compose::Session#port

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

#port(service, port, protocol: 'tcp', index: 1) ⇒ String?

Figure out which interface(s) and port a given service port has been published to.

NOTE: if Docker Compose is communicating with a remote Docker host, this method returns IP addresses from the point of view of that host and its interfaces. If you need to know the address as reachable from localhost, you probably want to use ‘Mapper`.

Parameters:

  • service (String)

    name of service from docker-compose.yml

  • port (Integer)

    number of port

  • protocol (String) (defaults to: 'tcp')

    ‘tcp’ or ‘udp’

  • index (Integer) (defaults to: 1)

    of container (if multiple instances running)

Returns:

  • (String, nil)

    an ip:port pair such as “0.0.0.0:32176” or nil if the service is not running

Raises:

  • (Error)

    if command fails

See Also:

[View source]

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/docker/compose/session.rb', line 201

def port(service, port, protocol: 'tcp', index: 1)
  inter = @shell.interactive
  @shell.interactive = false

  o = opts(protocol: [protocol, 'tcp'], index: [index, 1])
  s = strip_ansi(run!('port', o, service, port).strip)
  (!s.empty? && s) || nil
rescue Error => e
  # Deal with docker-compose v1.11+
  if e.detail =~ /No container found/i
    nil
  else
    raise
  end
ensure
  @shell.interactive = inter
end