From e0a4c58081db9c1dfb4f63c8ca0bb0594e327284 Mon Sep 17 00:00:00 2001 From: Mike Foley Date: Thu, 2 Feb 2017 20:18:12 -0500 Subject: [PATCH] Added service name to dockerData struct. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In Swarm mode with with Docker Swarm’s Load Balancer disabled (traefik.backend.loadbalancer.swarm=false) service name will be the name of the docker service and name will be the container task name (e.g. whoami0.1). When generating backend and fronted rules, we will use service name instead of name if a rule is not provided. Initialize dockerData.ServiceName to dockerData.Name to support non-swarm mode. --- docs/user-guide/swarm-mode.md | 4 ---- provider/docker.go | 8 ++++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/user-guide/swarm-mode.md b/docs/user-guide/swarm-mode.md index 289cc2992..5102ddcf9 100644 --- a/docs/user-guide/swarm-mode.md +++ b/docs/user-guide/swarm-mode.md @@ -120,16 +120,12 @@ docker-machine ssh manager "docker service create \ --name whoami0 \ --label traefik.port=80 \ --network traefik-net \ - --label traefik.frontend.rule=Host:whoami0.traefik \ - --label traefik.backend=whoami0 \ emilevauge/whoami" docker-machine ssh manager "docker service create \ --name whoami1 \ --label traefik.port=80 \ --network traefik-net \ - --label traefik.frontend.rule=Host:whoam1.traefik \ - --label traefik.backend=whoami1 \ --label traefik.backend.loadbalancer.sticky=true \ emilevauge/whoami" ``` diff --git a/provider/docker.go b/provider/docker.go index 9a9211c43..2ffeb41ac 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -52,6 +52,7 @@ type Docker struct { // dockerData holds the need data to the Docker provider type dockerData struct { + ServiceName string Name string Labels map[string]string // List of labels set to container or service NetworkSettings networkSettings @@ -393,14 +394,14 @@ func (provider *Docker) getFrontendRule(container dockerData) string { if label, err := getLabel(container, "traefik.frontend.rule"); err == nil { return label } - return "Host:" + provider.getSubDomain(container.Name) + "." + provider.Domain + return "Host:" + provider.getSubDomain(container.ServiceName) + "." + provider.Domain } func (provider *Docker) getBackend(container dockerData) string { if label, err := getLabel(container, "traefik.backend"); err == nil { return normalize(label) } - return normalize(container.Name) + return normalize(container.ServiceName) } func (provider *Docker) getIPAddress(container dockerData) string { @@ -559,6 +560,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. if container.ContainerJSONBase.HostConfig != nil { dockerData.NetworkSettings.NetworkMode = container.ContainerJSONBase.HostConfig.NetworkMode @@ -641,6 +643,7 @@ func (provider *Docker) listServices(ctx context.Context, dockerClient client.AP func parseService(service swarmtypes.Service, networkMap map[string]*dockertypes.NetworkResource) dockerData { dockerData := dockerData{ + ServiceName: service.Spec.Annotations.Name, Name: service.Spec.Annotations.Name, Labels: service.Spec.Annotations.Labels, NetworkSettings: networkSettings{}, @@ -691,6 +694,7 @@ func listTasks(ctx context.Context, dockerClient client.APIClient, serviceID str func parseTasks(task swarmtypes.Task, serviceDockerData dockerData, networkMap map[string]*dockertypes.NetworkResource) dockerData { dockerData := dockerData{ + ServiceName: serviceDockerData.Name, Name: serviceDockerData.Name + "." + strconv.Itoa(task.Slot), Labels: serviceDockerData.Labels, NetworkSettings: networkSettings{},