Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Host environment variables with docker stack deploy

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?

like image 576
Alessandro Dionisi Avatar asked Feb 28 '18 09:02

Alessandro Dionisi


People also ask

How do I pass environment variables to docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

Can docker container access host environment variables?

You can pass the values of environment variables from the host to your containers without much effort. Simply don't specify a value in the command line, and make sure that the environment variable is named the same as the variable the containerized app expects: $ docker run -e var_name (...)

Is it possible to pass environment variables using Dockerfiles?

Passing Environment Variables Into a DockerfileDockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

Do docker container inherit environment variables?

Using docker-compose , you can inherit env variables in docker-compose. yml and subsequently any Dockerfile(s) called by docker-compose to build images. This is useful when the Dockerfile RUN command should execute commands specific to the environment.


2 Answers

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
like image 173
Bret Fisher Avatar answered Sep 27 '22 21:09

Bret Fisher


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.

like image 35
Tim Avatar answered Sep 27 '22 20:09

Tim