1

I have a very big file which needs to be passed as an Environment variable to Docker run command in linux ec2 platform.

My ideal docker command looks like this :

docker run -p 6080:9000 -e KAFKA_BROKERCONNECT=url:port -e KAFKA_TRUSTSTORE="$(cat directory/kafka.client.truststore.jks | base64)" -e KAFKA_KEYSTORE="$(cat directory/kafka.client.keystore.jks | base64)" -e KAFKA_PROPERTIES="$(cat directory/kafka.properties | base64)" -e SERVER_SERVLET_CONTEXTPATH="/" -d obsidiandynamics/kafdrop

I get /bin/docker : Argument list too long

So instead of doing the above, i passed all the Env variables from the file kafdrop-properties file looks like :

KAFKA_BROKERCONNECT=host:port
KAFKA_TRUSTSTORE="$(cat 2kafka.client.truststore.jks)"
KAFKA_KEYSTORE="$(cat 2kafka.client.keystore.jks)"
KAFKA_PROPERTIES="$(cat /home/de601928/kafka/kafka.properties | base64)

and the corresponding Docker run command :

docker run -p 6080:9000 --env-file kafdrop-properties -d obsidiandynamics/kafdrop

Now after i run the command, i still see the enviroment variables like KAFKA_TRUSTSTORE are not reading via the cat content and displays as it is when i run docker exec env

Why isnt the cat command not reading from the file and passing the value as the Environment variable argument .

I have tried different ways like giving "<file" , etc. Appreciate any help here.

  • Did you `export` those variables? Also - what do the strings that `cat directory/kafka.client.truststore.jks | base64` and such generate look like? with the base64 they shouldn't include spaces, which makes the error message hard to fathom ... – tink Nov 03 '20 at 16:45

1 Answers1

0

That happens because docker uses a simplified mechanism of parsing .env files as described here. Variables are parsed only when referenced directly, and commands are not supported.

EDIT:

Using docker-compose something like this should work:

Your docker-compose.yml file

version: '3'
services:
  my-service:
    image: obsidiandynamics/kafdrop
    environment:
      - KAFKA_BROKERCONNECT
      - KAFKA_TRUSTSTORE
      - KAFKA_KEYSTORE
      - KAFKA_PROPERTIES
    ports:
      - 6080:9000

Then run

eval $(egrep -v '^#' .env | xargs) && docker-compose up

What should happen is that, the first part of command will parse the file and set the variables, and then docker-compose command will be run while having those variables evaluated.

Now, by setting environment section of docker-compose.yml like above, those specific variables will be passed from the shell to the container.

Milan Markovic
  • 1,250
  • 13
  • 18
  • Thanks @Milan for the info. In that case there is no way i can pass an environment variable to the docker ? Is there any other workaround ? – DEEPAK R.L. Nov 05 '20 at 17:31
  • A workaround could be to preprocess the env file with bash first, but I'm not sure how to pass them using run command. If you could use docker-compose then you would be able to pass them in directly from shell, because docker-compose.yml files can pass environment variables from shell to the container. If you like I can probably write you an example – Milan Markovic Nov 05 '20 at 18:16
  • Sure. will give it a try. But the problem is that, this is a docker image i am pulling from the repo as mentioned in https://hub.docker.com/r/obsidiandynamics/kafdrop and there arent examples using docker compose to pass these environment variables without changing the source code. – DEEPAK R.L. Nov 05 '20 at 18:44