Merge pull request #1235 from tcoupin/feat-use-docker-compose-labels

Use docker-compose labels for frontend and backend names
This commit is contained in:
Emile Vauge 2017-04-03 11:46:20 +02:00 committed by GitHub
commit 3389908238
3 changed files with 49 additions and 1 deletions

View file

@ -823,7 +823,7 @@ Labels can be used on containers to override default behaviour:
- `traefik.protocol=https`: override the default `http` protocol - `traefik.protocol=https`: override the default `http` protocol
- `traefik.weight=10`: assign this weight to the container - `traefik.weight=10`: assign this weight to the container
- `traefik.enable=false`: disable this container in Træfɪk - `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:{service}.{project_name}.{domain}` if you are using `docker-compose`).
- `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend. - `traefik.frontend.passHostHeader=true`: forward client `Host` header to the backend.
- `traefik.frontend.priority=10`: override default frontend priority - `traefik.frontend.priority=10`: override default frontend priority
- `traefik.frontend.entryPoints=http,https`: assign this frontend to entry points `http` and `https`. Overrides `defaultEntryPoints`. - `traefik.frontend.entryPoints=http,https`: assign this frontend to entry points `http` and `https`. Overrides `defaultEntryPoints`.

View file

@ -525,6 +525,10 @@ func (provider *Docker) getFrontendRule(container dockerData) string {
if label, err := getLabel(container, "traefik.frontend.rule"); err == nil { if label, err := getLabel(container, "traefik.frontend.rule"); err == nil {
return label 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.service"]+"."+labels["com.docker.compose.project"]) + "." + provider.Domain
}
return "Host:" + provider.getSubDomain(container.ServiceName) + "." + 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 { if label, err := getLabel(container, "traefik.backend"); err == nil {
return normalize(label) 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.service"] + "_" + labels["com.docker.compose.project"])
}
return normalize(container.ServiceName) return normalize(container.ServiceName)
} }

View file

@ -44,6 +44,20 @@ func TestDockerGetFrontendName(t *testing.T) {
}, },
expected: "Headers-User-Agent-bat-0-1-0", 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-bar-foo-docker-localhost",
},
{ {
container: docker.ContainerJSON{ container: docker.ContainerJSON{
ContainerJSONBase: &docker.ContainerJSONBase{ ContainerJSONBase: &docker.ContainerJSONBase{
@ -133,6 +147,19 @@ func TestDockerGetFrontendRule(t *testing.T) {
}, },
}, },
expected: "Host:foo.bar", 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:bar.foo.docker.localhost",
}, },
{ {
container: docker.ContainerJSON{ container: docker.ContainerJSON{
@ -196,6 +223,20 @@ func TestDockerGetBackend(t *testing.T) {
}, },
expected: "foobar", 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: "bar-foo",
},
} }
for _, e := range containers { for _, e := range containers {