Docker swarm service per replica environment variable

63 views Asked by At

Is there a way so set up environment variables per replica when running containers in docker swarm?

So, basically, I need to identify somehow which container instance the replica is running on from inside docker container (Spring boot app) when running through docker service create command or docker compose.

By design, I can not access docker host name or IP.

Thank you.

1

There are 1 answers

0
BMitch On

Templates are available for swarm services: https://docs.docker.com/reference/cli/docker/service/create/#create-services-using-templates

For this, you can specify an environment variable like INSTANCE={{.Task.Slot}} to know which container instance is being run (a number), or NODE={{.Node.Hostname}} to inject the node name if each instance runs on a separate node in the cluster. This works for services defined with a compose file or individually with docker service create.