7

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?

pjotr_dolphin
  • 1,207
  • 9
  • 34

1 Answers1

10

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

pjotr_dolphin
  • 1,207
  • 9
  • 34