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.
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), orNODE={{.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 withdocker service create.