Host environment variables with docker stack deploy

后端 未结 2 1867
無奈伤痛
無奈伤痛 2021-02-06 02:55

I was wondering if there is a way to use environment variables taken from the host where the container is deployed, instead of the ones taken from where the docker stack d

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 03:53

    Yes, this works when you combine two concepts:

    1. Swarm node labels, of which Hostname is one of the built-in ones.
    2. Swarm service go templates, which also work in stack files.

    This would pull in the hostname to the ENV value of DUDE for each container to be the host that it's running on:

    version: '3.4'
    
    services:
      nginx:
        image: nginx
        environment:
          DUDE: "{{.Node.Hostname}}"
        deploy:
          replicas: 3
    

提交回复
热议问题