Host environment variables with docker stack deploy

后端 未结 2 1865
無奈伤痛
無奈伤痛 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:45

    It works if you run the docker command through env.

    env JMX_HOSTNAME="${JMX_HOSTNAME}" docker stack deploy -c docker-compose.yml mystack
    

    Credit to GitHub issue that pointed me in the right direction.

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题