Search code examples
dockerdocker-swarmservice-discovery

Discoverabilty of Tasks in Docker Swarm


I am running a small Docker Swarm running a service that has 2 replicas. Within that service, the "task-1" of this service need to talk to the "task-2" of the same service, but I don't find a way to achieve this

First, I want to avoid to set the service in Host Networking Mode, because then it makes very few sense to put this into the swarm.

Second, what I figured out that you can set a environment variable that contains the TaskName in the docker-compose.yml file that I deploy to the swarm with docker stack deply ...:

    environment:
        - NODENAME={{.Node.Hostname}}
        - NODEID={{.Node.ID}}
        - SERVICEID={{.Service.ID}}  
        - SERVICENAME={{.Service.Name}}  
        - TASKID={{.Task.ID}}
        - TASKNAME={{.Task.Name}}

You can ping the $TASKNAME from various containers. but it is not discoverable, because the name TASKNAME=e2foobar_yada.gq7ygzvp114q2x3t99lasuowc.e6ncft2k14g9o2u4blvhns19 contains IDs that are changing when you restart the service.

Can I set an alias or is there any approach that allows me to task communication?


Solution

  • You can run a DNS lookup on tasks.$servicename where $servicename is the name of your service. It will resolve to a list of ip's pointing to each task in the service. It is DNS-RR implemented for swarm mode to support a process that cannot go through the built in IP based RR load balancer.

    You will want to query the DNS list every time you go to access your service since containers can be replaced. There's a risk DNS will become stale and you will talk to a down container or a completely different container (this is why IP based load balancing is so popular). And you will also need to exclude yourself from the list of returned IPs.