From 0a7f9b5a71201517f85f305e8b1ea2138a0b4e16 Mon Sep 17 00:00:00 2001 From: tcoupin Date: Sat, 4 Mar 2017 13:31:07 +0100 Subject: [PATCH 1/2] Use docker-compose labels for frontend and backend names --- docs/toml.md | 2 +- provider/docker.go | 7 +++++++ provider/docker_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/docs/toml.md b/docs/toml.md index 3c9426671..574efd39a 100644 --- a/docs/toml.md +++ b/docs/toml.md @@ -823,7 +823,7 @@ Labels can be used on containers to override default behaviour: - `traefik.protocol=https`: override the default `http` protocol - `traefik.weight=10`: assign this weight to the container - `traefik.enable=false`: disable this container in Træfɪk -- `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}`). +- `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}` or `Host:{project_name}-{service}.{domain}` if you are using `docker-compose`). - `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend. - `traefik.frontend.priority=10`: override default frontend priority - `traefik.frontend.entryPoints=http,https`: assign this frontend to entry points `http` and `https`. Overrides `defaultEntryPoints`. diff --git a/provider/docker.go b/provider/docker.go index 106959ee7..3c309d178 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -525,6 +525,10 @@ func (provider *Docker) getFrontendRule(container dockerData) string { if label, err := getLabel(container, "traefik.frontend.rule"); err == nil { return label } + if labels, err := getLabels(container, []string{"com.docker.compose.project", "com.docker.compose.service"}); err == nil { + return "Host:" + provider.getSubDomain(labels["com.docker.compose.project"]+"_"+labels["com.docker.compose.service"]) + "." + provider.Domain + } + return "Host:" + provider.getSubDomain(container.ServiceName) + "." + provider.Domain } @@ -532,6 +536,9 @@ func (provider *Docker) getBackend(container dockerData) string { if label, err := getLabel(container, "traefik.backend"); err == nil { return normalize(label) } + if labels, err := getLabels(container, []string{"com.docker.compose.project", "com.docker.compose.service"}); err == nil { + return normalize(labels["com.docker.compose.project"] + "_" + labels["com.docker.compose.service"]) + } return normalize(container.ServiceName) } diff --git a/provider/docker_test.go b/provider/docker_test.go index c1222fc7a..4347ee79c 100644 --- a/provider/docker_test.go +++ b/provider/docker_test.go @@ -44,6 +44,20 @@ func TestDockerGetFrontendName(t *testing.T) { }, expected: "Headers-User-Agent-bat-0-1-0", }, + { + container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "mycontainer", + }, + Config: &container.Config{ + Labels: map[string]string{ + "com.docker.compose.project": "foo", + "com.docker.compose.service": "bar", + }, + }, + }, + expected: "Host-foo-bar-docker-localhost", + }, { container: docker.ContainerJSON{ ContainerJSONBase: &docker.ContainerJSONBase{ @@ -133,6 +147,19 @@ func TestDockerGetFrontendRule(t *testing.T) { }, }, expected: "Host:foo.bar", + }, { + container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "test", + }, + Config: &container.Config{ + Labels: map[string]string{ + "com.docker.compose.project": "foo", + "com.docker.compose.service": "bar", + }, + }, + }, + expected: "Host:foo-bar.docker.localhost", }, { container: docker.ContainerJSON{ @@ -196,6 +223,20 @@ func TestDockerGetBackend(t *testing.T) { }, expected: "foobar", }, + { + container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "test", + }, + Config: &container.Config{ + Labels: map[string]string{ + "com.docker.compose.project": "foo", + "com.docker.compose.service": "bar", + }, + }, + }, + expected: "foo-bar", + }, } for _, e := range containers { From 5c16860486826e38e0d0c27f40fef20bb8307594 Mon Sep 17 00:00:00 2001 From: tcoupin Date: Thu, 9 Mar 2017 22:27:09 +0100 Subject: [PATCH 2/2] Use service.project.domain instead of project-service.domain --- docs/toml.md | 2 +- provider/docker.go | 4 ++-- provider/docker_test.go | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/toml.md b/docs/toml.md index 574efd39a..e49f82e27 100644 --- a/docs/toml.md +++ b/docs/toml.md @@ -823,7 +823,7 @@ Labels can be used on containers to override default behaviour: - `traefik.protocol=https`: override the default `http` protocol - `traefik.weight=10`: assign this weight to the container - `traefik.enable=false`: disable this container in Træfɪk -- `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}` or `Host:{project_name}-{service}.{domain}` if you are using `docker-compose`). +- `traefik.frontend.rule=Host:test.traefik.io`: override the default frontend rule (Default: `Host:{containerName}.{domain}` or `Host:{service}.{project_name}.{domain}` if you are using `docker-compose`). - `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend. - `traefik.frontend.priority=10`: override default frontend priority - `traefik.frontend.entryPoints=http,https`: assign this frontend to entry points `http` and `https`. Overrides `defaultEntryPoints`. diff --git a/provider/docker.go b/provider/docker.go index 3c309d178..9cc57b17d 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -526,7 +526,7 @@ func (provider *Docker) getFrontendRule(container dockerData) string { return label } if labels, err := getLabels(container, []string{"com.docker.compose.project", "com.docker.compose.service"}); err == nil { - return "Host:" + provider.getSubDomain(labels["com.docker.compose.project"]+"_"+labels["com.docker.compose.service"]) + "." + provider.Domain + return "Host:" + provider.getSubDomain(labels["com.docker.compose.service"]+"."+labels["com.docker.compose.project"]) + "." + provider.Domain } return "Host:" + provider.getSubDomain(container.ServiceName) + "." + provider.Domain @@ -537,7 +537,7 @@ func (provider *Docker) getBackend(container dockerData) string { return normalize(label) } if labels, err := getLabels(container, []string{"com.docker.compose.project", "com.docker.compose.service"}); err == nil { - return normalize(labels["com.docker.compose.project"] + "_" + labels["com.docker.compose.service"]) + return normalize(labels["com.docker.compose.service"] + "_" + labels["com.docker.compose.project"]) } return normalize(container.ServiceName) } diff --git a/provider/docker_test.go b/provider/docker_test.go index 4347ee79c..ac614ae1c 100644 --- a/provider/docker_test.go +++ b/provider/docker_test.go @@ -56,7 +56,7 @@ func TestDockerGetFrontendName(t *testing.T) { }, }, }, - expected: "Host-foo-bar-docker-localhost", + expected: "Host-bar-foo-docker-localhost", }, { container: docker.ContainerJSON{ @@ -159,7 +159,7 @@ func TestDockerGetFrontendRule(t *testing.T) { }, }, }, - expected: "Host:foo-bar.docker.localhost", + expected: "Host:bar.foo.docker.localhost", }, { container: docker.ContainerJSON{ @@ -235,7 +235,7 @@ func TestDockerGetBackend(t *testing.T) { }, }, }, - expected: "foo-bar", + expected: "bar-foo", }, }