How to set hostname in global service in Docker Swarm

夙愿已清 提交于 2020-07-19 04:26:19

问题


I have a service deployed to my Docker Swarm Cluster as global service (ELK Metricbeat).

I want to each of this service to have a hostname the same as the hostname of the running node (host)?

in another word, how I can achieve the same result in the yml file such as:

 docker run -h `hostname` elastic/metricbeat:5.4.1

this is my yml file:

metricbeat:
  image: elastic/metricbeat:5.4.1
  command: metricbeat -e -c /etc/metricbeat/metricbeat.yml -system.hostfs=/hostfs
  hostname: '`hostname`'
  volumes:
    - /proc:/hostfs/proc:ro
    - /sys/fs/cgroup:/hostfs/sys/fs/cgroup:ro
    - /:/hostfs:ro
    - /var/run/docker.sock:/var/run/docker.sock
  networks:
    - net
  user: root
  deploy:
    mode: global

I have tried:

  hostname: '`hostname`'
  hostname: '${hostname}'

but no success.

Any solution?

Thank you in advance.


回答1:


For anyone coming here :

services:
  myservice:
    hostname: "{{.Node.Hostname}}-{{.Service.Name}}"

No need to alter entry point ( at least on swarm on deploy )




回答2:


I resolved the issue by mounting the host hostname file under /etc/nodehostname and changing the service container to use an entrypoint that read the file and replace a variable (name) in metricbeat.yml

docker-entrypoint.sh

export NODE_HOSTNAME=$(eval cat /etc/nodehostname)

envsubst '$NODE_HOSTNAME' </etc/metricbeat/metricbeat.yml.tpl > /etc/metricbeat/metricbeat.yml


来源:https://stackoverflow.com/questions/44385375/how-to-set-hostname-in-global-service-in-docker-swarm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!