使用Docker Stack部署主机环境变量 [英] Host environment variables with docker stack deploy

查看:720
本文介绍了使用Docker Stack部署主机环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以使用从部署容器的主机获取的环境变量,而不是从执行docker stack deploy命令的主机获取的环境变量.例如,想象以下在三个节点的Docker Swarm集群上启动的docker-compose.yml:

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"

JMX_HOSTNAME应该从实际部署容器的主机获取,并且每个容器的值都不应相同. 有正确的方法吗?

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?

推荐答案

是的,当您结合两个概念时,此方法有效:

Yes, this works when you combine two concepts:

  1. 群集节点标签,其中主机名是内置的在一个.
  2. 蜂群服务转到模板,它也在堆栈文件中工作.
  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.

这会将主机名拉到DUDE的ENV值,以使每个容器成为其运行的主机:

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

这篇关于使用Docker Stack部署主机环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆