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:



172
173
174
175
176
177
178
179
180
181
# File 'lib/docker/compose/session.rb', line 172

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
ensure
  @shell.interactive = inter
end