diff --git a/docs/toml.md b/docs/toml.md index 7355cf6ba..9689fafa6 100644 --- a/docs/toml.md +++ b/docs/toml.md @@ -584,7 +584,8 @@ Labels can be used on containers to override default behaviour: - `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}`). - `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend. - `traefik.frontend.entryPoints=http,https`: assign this frontend to entry points `http` and `https`. Overrides `defaultEntryPoints`. -* `traefik.domain=traefik.localhost`: override the default domain +- `traefik.domain=traefik.localhost`: override the default domain +- `traefik.docker.network`: Set the docker network to use for connections to this container ## Marathon backend @@ -689,7 +690,7 @@ Træfɪk can be configured to use Kubernetes Ingress as a backend configuration: # and KUBERNETES_SERVICE_PORT_HTTPS as endpoint # Secure token will be found in /var/run/secrets/kubernetes.io/serviceaccount/token # and SSL CA cert in /var/run/secrets/kubernetes.io/serviceaccount/ca.crt -# +# # Optional # # endpoint = "http://localhost:8080" @@ -1025,4 +1026,3 @@ Once the `/traefik/alias` key is updated, the new `/traefik_configurations/2` co | `/traefik_configurations/2/backends/backend1/servers/server2/weight` | `5` | Note that Træfɪk *will not watch for key changes in the `/traefik_configurations` prefix*. It will only watch for changes in the `/traefik` prefix. Further, if the `/traefik/alias` key is set, all other sibling keys with the `/traefik` prefix are ignored. - diff --git a/provider/docker.go b/provider/docker.go index f04a82c03..542f244ab 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -161,6 +161,7 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po func (provider *Docker) loadDockerConfig(containersInspected []dockertypes.ContainerJSON) *types.Configuration { var DockerFuncMap = template.FuncMap{ "getBackend": provider.getBackend, + "getIPAddress": provider.getIPAddress, "getPort": provider.getPort, "getWeight": provider.getWeight, "getDomain": provider.getDomain, @@ -245,6 +246,22 @@ func (provider *Docker) getBackend(container dockertypes.ContainerJSON) string { return normalize(container.Name) } +func (provider *Docker) getIPAddress(container dockertypes.ContainerJSON) string { + if label, err := getLabel(container, "traefik.docker.network"); err == nil && label != "" { + networks := container.NetworkSettings.Networks + if networks != nil { + network := networks[label] + if network != nil { + return network.IPAddress + } + } + } + for _, network := range container.NetworkSettings.Networks { + return network.IPAddress + } + return "" +} + func (provider *Docker) getPort(container dockertypes.ContainerJSON) string { if label, err := getLabel(container, "traefik.port"); err == nil { return label diff --git a/provider/docker_test.go b/provider/docker_test.go index 417a120d4..3605d280a 100644 --- a/provider/docker_test.go +++ b/provider/docker_test.go @@ -203,6 +203,82 @@ func TestDockerGetBackend(t *testing.T) { } } +func TestDockerGetIPAddress(t *testing.T) { // TODO + provider := &Docker{} + + containers := []struct { + container docker.ContainerJSON + expected string + }{ + { + container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "bar", + }, + Config: &container.Config{}, + NetworkSettings: &docker.NetworkSettings{ + Networks: map[string]*network.EndpointSettings{ + "testnet": { + IPAddress: "10.11.12.13", + }, + }, + }, + }, + expected: "10.11.12.13", + }, + { + container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "bar", + }, + Config: &container.Config{ + Labels: map[string]string{ + "traefik.docker.network": "testnet", + }, + }, + NetworkSettings: &docker.NetworkSettings{ + Networks: map[string]*network.EndpointSettings{ + "nottestnet": { + IPAddress: "10.11.12.13", + }, + }, + }, + }, + expected: "10.11.12.13", + }, + { + container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "bar", + }, + Config: &container.Config{ + Labels: map[string]string{ + "traefik.docker.network": "testnet2", + }, + }, + NetworkSettings: &docker.NetworkSettings{ + Networks: map[string]*network.EndpointSettings{ + "testnet1": { + IPAddress: "10.11.12.13", + }, + "testnet2": { + IPAddress: "10.11.12.14", + }, + }, + }, + }, + expected: "10.11.12.14", + }, + } + + for _, e := range containers { + actual := provider.getIPAddress(e.container) + if actual != e.expected { + t.Fatalf("expected %q, got %q", e.expected, actual) + } + } +} + func TestDockerGetPort(t *testing.T) { provider := &Docker{} diff --git a/templates/docker.tmpl b/templates/docker.tmpl index f43bef8b4..08e1f54b5 100644 --- a/templates/docker.tmpl +++ b/templates/docker.tmpl @@ -1,6 +1,6 @@ [backends]{{range .Containers}} [backends.backend-{{getBackend .}}.servers.server-{{.Name | replace "/" "" | replace "." "-"}}] - url = "{{getProtocol .}}://{{range $i := .NetworkSettings.Networks}}{{if $i}}{{.IPAddress}}{{end}}{{end}}:{{getPort .}}" + url = "{{getProtocol .}}://{{getIPAddress .}}:{{getPort .}}" weight = {{getWeight .}} {{end}}