Skip to content

Commit

Permalink
Use of container.HostConfig.NetworkMode to detect host mode
Browse files Browse the repository at this point in the history
Signed-off-by: Emile Vauge <emile@vauge.com>
  • Loading branch information
emilevauge committed Jul 5, 2016
1 parent 6fd8979 commit b1ecb1f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
14 changes: 8 additions & 6 deletions provider/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
24 changes: 24 additions & 0 deletions provider/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b1ecb1f

Please sign in to comment.