Search code examples
docker-swarmswarmdocker-swarm-mode

Get swarm node name from container


I have swarm nodes and we are deploying containers, from the containers I want to update records in database, like I want to update in my database, which node serve the request.

Is there any way to know using environment variable or something else, by which we know from container the node name ?


Solution

  • Actualy, you can use the service template variable '{{.Node.Hostname}}' to render the host name into an environment variable und levarage it within your application.

    A while ago, I posted examples of swarm service template variables in the docker forum: https://forums.docker.com/t/example-usage-of-docker-swarm-template-placeholders/73859