Docker Swarm Unique Custom Hostname for Service in case of scale up

How do I make unique hostname in case of scale up ?.

I did tried appending this {{.Config.Hostname}} but this does not work and gave error.
failed to create service docker-trino_trino-worker: Error response from daemon: rpc error: code = InvalidArgument desc = expanding hostname failed: template: expansion:1:41: executing "expansion" at <.Config.Hostname>: can't evaluate field Config in type *template.Context

For Service Trino-Worker, I need to have unique hostname when scaling up

hostname: "Trino-Worker-{{.Node.Hostname}}"
cat swarm-compose.yml 
version: "3.8"
services:
  hive-metastore:
    build: 
      dockerfile: ../hive-metastore/Dockerfile
      context: ../hive-metastore
    image: 10.208.1.5:5000/hive-metastore:3.1.3
    hostname: hivemetastore
    networks: 
      trino-network-docker:
        aliases:
          - hivemetastore
    environment:
      - DATABASE_HOST=mysql-hivemeta.aws.com
      - PROJECT_NAME=${COMPOSE_PROJECT_NAME}
    ports:
      - "9083:9083"
    restart: unless-stopped
    extra_hosts:
      - "host.docker.internal:host-gateway"

  hiveservice2:
    build: 
      dockerfile: ../hive/Dockerfile
      context: ../hive
    image: 10.208.1.5:5000/myhive:latest
    networks:
      - trino-network-docker
    environment:
      - DATABASE_HOST=mysql-hivemeta.aws.com
      - PROJECT_NAME=${COMPOSE_PROJECT_NAME}
      - IS_RESUME="TRUE"
      - HIVE_CUSTOM_CONF_DIR=/hive_custom_conf
      - DB_DRIVER=mysql
      - HIVE_METASTORE_URI=${HIVE_METASTORE_URI}
      - SERVICE_OPTS=-Dhive.metastore.uris=${HIVE_METASTORE_URI}
      - VERBOSE="true"
      - SERVICE_NAME=hiveserver2
    volumes:
      - ../hive/conf/:/hive_custom_conf
    ports:
      - "10000:10000"
      - "10002:10002"
    restart: unless-stopped
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
       - hive-metastore  
  trino-master:
    build: 
      dockerfile: ../trino/Dockerfile
      context: ../trino
    image: 10.208.1.5:5000/trinodb/trino:latest 
    hostname: trinomaster
    networks: 
      trino-network-docker:
        aliases:
          - trinomaster
    volumes:
      - ../trino/master/conf/:/etc/trino/
      - ../trino/data:/data/trino/
     #- /.aws/:/root/.aws/
     #- /.aws/:/home/trino/.aws/
    ports:
      - "8080:8080"
    restart: unless-stopped
    extra_hosts:
      - "host.docker.internal:host-gateway"
    environment:
      - PROJECT_NAME=${COMPOSE_PROJECT_NAME}
      - DISCOVERY_URI=${DISCOVERY_URI}
      - HIVE_METASTORE_URI=${HIVE_METASTORE_URI}
      - INCLUDE_COORDINATOR=${INCLUDE_COORDINATOR}
    command: sh -c 'sed -i "/coordinator=/d" /etc/trino/config.properties && 
                    echo coordinator=true >> /etc/trino/config.properties && 
                    sed -i "/discovery.uri/d" /etc/trino/config.properties && 
                    echo discovery.uri=${DISCOVERY_URI} >> /etc/trino/config.properties && 
                    sed -i "/hive.metastore.uri/d" /etc/trino/catalog/hive.properties && 
                    echo hive.metastore.uri=${HIVE_METASTORE_URI} >> /etc/trino/catalog/hive.properties && 
                    sed -i "/node-scheduler.include-coordinator/d" /etc/trino/config.properties && 
                    echo node-scheduler.include-coordinator=${INCLUDE_COORDINATOR} >> /etc/trino/config.properties && 
                    sed -i "/discovery-server.enabled/d" /etc/trino/config.properties && 
                    echo discovery-server.enabled=true >> /etc/trino/config.properties && 
                    /usr/lib/trino/bin/run-trino'
    healthcheck:
      test: /usr/lib/trino/bin/health-check 
      interval: 60s
      retries: 5
      start_period: 30s
      timeout: 30s

    depends_on:
       - hive-metastore  
  trino-worker:
    build: 
      dockerfile: ../trino/Dockerfile
      context: ../trino
    image: 10.208.1.5:5000/trinodb/trino:latest 
    hostname: "Trino-Worker-{{.Node.Hostname}}"
    networks:
      - trino-network-docker
    volumes:
      - ../trino/worker/conf/:/etc/trino/
      - ../trino/master/conf/catalog/:/etc/trino/catalog
      - ../trino/data:/data/trino/
     #- /.aws/:/root/.aws/
     #- /.aws/:/home/trino/.aws/
    ports:
      - "8081-8099:8080"
    restart: unless-stopped
    extra_hosts:
      - "host.docker.internal:host-gateway"
    environment:
      - PROJECT_NAME=${COMPOSE_PROJECT_NAME}
      - DISCOVERY_URI=${DISCOVERY_URI}
      - HIVE_METASTORE_URI=${HIVE_METASTORE_URI}
      - INCLUDE_COORDINATOR=${INCLUDE_COORDINATOR}
    command: sh -c 'sed -i "/coordinator=/d" /etc/trino/config.properties && 
                    echo coordinator=false >> /etc/trino/config.properties && 
                    sed -i "/discovery.uri/d" /etc/trino/config.properties && 
                    echo discovery.uri=${DISCOVERY_URI} >> /etc/trino/config.properties && 
                    sed -i "/hive.metastore.uri/d" /etc/trino/catalog/hive.properties && 
                    echo hive.metastore.uri=${HIVE_METASTORE_URI} >> /etc/trino/catalog/hive.properties && 
                    sed -i "/node-scheduler.include-coordinator/d" /etc/trino/config.properties && 
                    echo node-scheduler.include-coordinator=${INCLUDE_COORDINATOR} >> /etc/trino/config.properties && 
                    /usr/lib/trino/bin/run-trino'
    healthcheck:
      test: /usr/lib/trino/bin/health-check 
      interval: 60s
      retries: 5
      start_period: 30s
      timeout: 30s

    depends_on:
       - hive-metastore  
       - trino-master
networks:
  trino-network-docker:
    name: trino-network-docker
    driver: overlay
    attachable: true

{{.Task.Slot}} seems to be solving my problem since each new task assign unique value when scaling up.

hostname: "Trino-Worker-{{.Node.Hostname}}-{{.Task.Slot}}"

Isn’t the hostname unique by default when using docker stack deploy?

And as mentioned before, build and depends_on don’t work with Swarm.

I like to (ab-)use command, too, for some file mods. But be aware that this will usually disable SIGTERM for the app, so it won’t be notified of the shutdown in 10 secs. This might lead to corrupted data when the app is writing.

Yeah, if we don’t specify then hostname is unique but I wanted to have hostname stated with prefix e.g. “Trino-Worker-” and I didn’t find example of using Hostname/container name in template. {{.Node.Hostname}} is not container hostname.
Yeah, I understand build and depends_on will work in docker-compose but not in docker swarm but having them in docker swarm/stack setup will get just ignored.

Hi Sanjay. Any luck figuring this one out? I was hoping to append the hostname of the docker node to the containers running in a swarm so that I can identify which node the container was running on in any of the logs. Cheers.

This should work

hostname: “Trino-Worker-{{.Node.Hostname}}-{{.Task.Slot}}”

This should work for global services, or replicated services with only one replica as well:

hostname: Trino-Worker-{{.Node.Hostname}}"

Years ago I played around with the template placeholders: