Search code examples
network-programmingdockerdocker-composedocker-swarm

How do I discover scaled container hosts with docker-compose+docker-swarm+overlay networking?


compose/docker-compose.yml

version: '2'

services:
    worker:
        image: some-image

    manager:
        image: some-image
        environment:
            # number of workers
            - INSTANCES=5

networks:
    default:
        driver: overlay

the workers are scaled with

docker-compose scale worker=5

The manager container is responsible for distributing the workload to the worker containers.

In order to achieve this, the manager container needs to know how many workers there are and what the hostnames are.

I know that I can reach the first worker container by using the host, "worker" or "compose_worker_1" and the second container by "compose_worker_2".

But how is manager supposed to know how many workers there are?

My current workaround is to specify the number of workers as an environment variable but it seems tedious having already run docker-compose scale.

Is there any other lightweight method I can use to discover the number of workers?


Solution

  • I would have the worker make a connection back to the manager after it starts and is ready to handle work, as a "registration". That way the manger doesn't need to know about anything, it just waits for workers to register themselves.