diff --git a/provider/docker.go b/provider/docker.go index 8e5f4cd09..9b732195e 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -270,12 +270,14 @@ func (provider *Docker) getIPAddress(container dockertypes.ContainerJSON) string } } } - for networkName, network := range container.NetworkSettings.Networks { - // If net==host, quick n' dirty, we return 127.0.0.1 - // This will work locally, but will fail with swarm. - if "host" == networkName { - return "127.0.0.1" - } + + // If net==host, quick n' dirty, we return 127.0.0.1 + // This will work locally, but will fail with swarm. + if container.HostConfig != nil && "host" == container.HostConfig.NetworkMode { + return "127.0.0.1" + } + + for _, network := range container.NetworkSettings.Networks { return network.IPAddress } return "" diff --git a/provider/docker_test.go b/provider/docker_test.go index 364f0e885..5dad8e030 100644 --- a/provider/docker_test.go +++ b/provider/docker_test.go @@ -269,6 +269,30 @@ func TestDockerGetIPAddress(t *testing.T) { // TODO }, expected: "10.11.12.14", }, + { + container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "bar", + HostConfig: &container.HostConfig{ + NetworkMode: "host", + }, + }, + Config: &container.Config{ + Labels: map[string]string{}, + }, + NetworkSettings: &docker.NetworkSettings{ + Networks: map[string]*network.EndpointSettings{ + "testnet1": { + IPAddress: "10.11.12.13", + }, + "testnet2": { + IPAddress: "10.11.12.14", + }, + }, + }, + }, + expected: "127.0.0.1", + }, } for _, e := range containers {