Get container IP for various networks

This commit is contained in:
Bryan Boreham committed 2015-11-30 14:27:33 +00:00
1 parent 85e465cfa4
commit 5eb99fe5ef
2 files changed
+15 -2

No files matched your search

+14 -1
View File
@@ -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
}
+1 -1
View File
@@ -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