Skip to content

Commit

Permalink
Use Node IP in Swarm Standalone with "host" NetworkMode
Browse files Browse the repository at this point in the history
  • Loading branch information
BlakeMesdag authored and traefiker committed Oct 25, 2017
1 parent 7c4353a commit d1d8b01
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 8 additions & 0 deletions provider/docker/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func networkMode(mode string) func(*docker.ContainerJSON) {
}
}

func nodeIP(ip string) func(*docker.ContainerJSON) {
return func(c *docker.ContainerJSON) {
c.ContainerJSONBase.Node = &docker.ContainerNode{
IPAddress: ip,
}
}
}

func labels(labels map[string]string) func(*docker.ContainerJSON) {
return func(c *docker.ContainerJSON) {
c.Config.Labels = labels
Expand Down
10 changes: 8 additions & 2 deletions provider/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type dockerData struct {
Labels map[string]string // List of labels set to container or service
NetworkSettings networkSettings
Health string
Node *dockertypes.ContainerNode
}

// NetworkSettings holds the networks data to the Provider p
Expand Down Expand Up @@ -596,9 +597,13 @@ func (p *Provider) getIPAddress(container dockerData) string {
}
}

// If net==host, quick n' dirty, we return 127.0.0.1
// This will work locally, but will fail with swarm.
if "host" == container.NetworkSettings.NetworkMode {
if container.Node != nil {
if container.Node.IPAddress != "" {
return container.Node.IPAddress
}
}

return "127.0.0.1"
}

Expand Down Expand Up @@ -823,6 +828,7 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData {
if container.ContainerJSONBase != nil {
dockerData.Name = container.ContainerJSONBase.Name
dockerData.ServiceName = dockerData.Name //Default ServiceName to be the container's Name.
dockerData.Node = container.ContainerJSONBase.Node

if container.ContainerJSONBase.HostConfig != nil {
dockerData.NetworkSettings.NetworkMode = container.ContainerJSONBase.HostConfig.NetworkMode
Expand Down
13 changes: 13 additions & 0 deletions provider/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ func TestDockerGetIPAddress(t *testing.T) {
),
expected: "127.0.0.1",
},
{
container: containerJSON(
networkMode("host"),
),
expected: "127.0.0.1",
},
{
container: containerJSON(
networkMode("host"),
nodeIP("10.0.0.5"),
),
expected: "10.0.0.5",
},
}

for containerID, e := range containers {
Expand Down

0 comments on commit d1d8b01

Please sign in to comment.