Host environment variables with docker stack deploy

两盒软妹~` 提交于 2019-12-04 12:13:48

问题


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 deploy command is executed. For example imagine the following docker-compose.yml launched on three node Docker Swarm cluster:

version: '3.2'
services:
  kafka:
    image: wurstmeister/kafka
    ports:
      - target: 9094
        published: 9094
        protocol: tcp
        mode: host
    deploy:
      mode: global
    environment:
      KAFKA_JMX_OPTS: "-Djava.rmi.server.hostname=${JMX_HOSTNAME} -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.rmi.port=1099"

The JMX_HOSTNAME should be taken from the host where the container is actually deployed and should not be the same value for every container. Is there a correct way to do this?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/49025686/host-environment-variables-with-docker-stack-deploy

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