Search code examples
dockerdocker-swarmdocker-stack

Stack service containers dynamic hostname


I am migrating away from Docker Cloud to pure Docker Swarm setup. One thing that I am missing is the nice way of how the containers got the hostname set as $SERVICE_NAME-$SLOT_NUMBER.

Is it possible to dynamically set the hostname/container name in a swarm stack service?


Solution

  • In your stack/compose file, use this format:

    services:
      thaservice:
        ...
        hostname: "{{.Service.Name}}-{{.Task.Slot}}"
    

    Link to documentation for available template variables: https://docs.docker.com/engine/swarm/services/#create-services-using-templates