Module: VagrantDockerHostsManager::Util::Docker

Defined in:
lib/vagrant-docker-hosts-manager/util/docker.rb

Class Method Summary collapse

Class Method Details

.ip_for_container(name) ⇒ String?

Resolves the first IPv4 address exposed by Docker inspect for a container.

Parameters:

  • name (String, #to_s)

    Docker container name or id.

Returns:

  • (String, nil)

    First IPv4 address, or nil when Docker cannot resolve it.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vagrant-docker-hosts-manager/util/docker.rb', line 15

def ip_for_container(name)
  return nil if name.to_s.strip.empty?

  fmt = "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}"
  Verbose.log("docker", "inspect", "-f", fmt, name.to_s)
  out, _err, status = Open3.capture3("docker", "inspect", "-f", fmt, name.to_s)
  return nil unless status.success?
  out.split(/\s+/).find { |ip| ip =~ /\A\d{1,3}(\.\d{1,3}){3}\z/ }
rescue StandardError
  nil
end