From 5eb99fe5efd90530a9bfd3e4606332bb83a6cfd2 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Thu, 26 Nov 2015 18:11:37 +0000 Subject: [PATCH] Get container IP for various networks --- common/docker/client.go | 15 ++++++++++++++- plugin/watcher.go | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/common/docker/client.go b/common/docker/client.go index ded610b3..f6de8981 100644 --- a/common/docker/client.go +++ b/common/docker/client.go @@ -81,12 +81,25 @@ func (c *Client) IsContainerNotRunning(idStr string) bool { return false } -func (c *Client) GetContainerBridgeIP(nameOrID string) (string, error) { +// This is intended to find an IP address that we can reach the container on; +// if it is on the Docker bridge network then that address; if on the host network +// then localhost +func (c *Client) GetContainerIP(nameOrID string) (string, error) { Log.Debugf("Getting IP for container %s", nameOrID) info, err := c.InspectContainer(nameOrID) if err != nil { return "", err } + if info.NetworkSettings.Networks != nil { + Log.Debugln("Networks: ", info.NetworkSettings.Networks) + if bridgeNetwork, ok := info.NetworkSettings.Networks["bridge"]; ok { + return bridgeNetwork.IPAddress, nil + } else if _, ok := info.NetworkSettings.Networks["host"]; ok { + return "127.0.0.1", nil + } + } else if info.HostConfig.NetworkMode == "host" { + return "127.0.0.1", nil + } return info.NetworkSettings.IPAddress, nil } diff --git a/plugin/watcher.go b/plugin/watcher.go index 701c53b9..c37b41e0 100644 --- a/plugin/watcher.go +++ b/plugin/watcher.go @@ -33,7 +33,7 @@ func NewWatcher(client *docker.Client) (Watcher, error) { func (w *watcher) haveWeaveClient() bool { if w.weave == nil { - dnsip, err := w.client.GetContainerBridgeIP(WeaveContainer) + dnsip, err := w.client.GetContainerIP(WeaveContainer) if err != nil { Log.Warningf("nameserver not available: %s", err) return false