Docker Community Forums

Share and learn in the Docker community.

Docker swarm mode with replicas: how to know container name inside

(Aldo Bongio) #1

Docker version 1.12.4 - swarm mode
Created a service “web” with 3 replicas.

docker service tasks web

ID                         NAME   SERVICE  IMAGE         LAST STATE                  DESIRED STATE  NODE
9tsk9hw2uac4tsharxkuzf981  web.1  web      nginx:alpine  Running about a minute ago  Running        moby
1opisbjf2ypok3cakoq2gmm4e  web.2  web      nginx:alpine  Running 6 seconds ago       Running        moby
3zqyv3sx6juzmlvxrcpl7adxo  web.3  web      nginx:alpine  Running 6 seconds ago       Running        moby

From inside one of the container (e.g. the second one) I need to code something (using a bash initialization script) which is able to retrieve the symbolic/pretty name (e.g. “web.2”).
In other words: is there something (some kind of DNS alias or other) that permits to each container to discover by itself (without using the Docker demon) which is the “pretty” name uniquely assigned to it by swarm ?


(Jean Traullé) #2

Hey @aldo ,

Did you find a solution to this ? I am also interested :stuck_out_tongue_winking_eye:

(Archimedes Trajano) #3

Not sure if it would work but based on Unique ENV var value per swarm replica instance you can try the following

- SLOT={{.Task.Slot}}

Which I am thinking that Slot is the replica number.