Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround to docker run "--env-file" supplied file not being evaluated as expected

Tags:

docker

My current setup for running a docker container is on the lines of this:

  1. I've got a main.env file:
# Main
export PRIVATE_IP=\`echo localhost\`
export MONGODB_HOST="$PRIVATE_IP"
export MONGODB_URL="mongodb://$MONGODB_HOST:27017/development"
  1. In my service file (upstart), I source this file . /path/to/main.env

  2. I then call docker run with multiple -e for each of the environment variables I want inside of the container. In this case I would call something like: docker run -e MONGODB_URL=$MONGODB_URL ubuntu bash

  3. I would then expect MONGODB_URL inside of the container to equal mongodb://localhost:27017/development. Notice that in reality echo localhost is replaced by a curl to amazon's api for an actual PRIVATE_IP.

This becomes a bit unwieldy when you start having more and more environment variables you need to give your container. There is a fine point to see here which is that the environment variables need to be resolved at run time, such as with a call to curl or by referring to other env variables.

The solution I was hoping to use is:

  1. calling docker run with an --env-file parameter such as this:
# Main
PRIVATE_IP=\`echo localhost\`
MONGODB_HOST="$PRIVATE_IP"
MONGODB_URL="mongodb://$MONGODB_HOST:27017/development"
  1. Then my docker run command would be significantly shortened to docker run --env-file=/path/to/main.env ubuntu bash (keep in mind usually I've got around 12-15 environment variables.

This is where I hit my problem which is that inside the container none of the variables resolve as expected. Instead I end up with:

  • PRIVATE_IP=`echo localhost`
  • MONGODB_HOST="$PRIVATE_IP"
  • MONGODB_URL="mongodb://$MONGODB_HOST:27017/development"

I could circumvent this by doing the following:

  1. Sourcing the main.env file.
  2. Creating a file containing just the names of the variables I want (meaning docker would search for them in the environment).
  3. Then calling docker run with this file as an argument to --env-file. This would work but would mean I would need to maintain two files instead of one, and really wouldn't be that big of an improvement of the current situation.

What I would prefer is to have the variables resolve as expected.

The closest question to mine that I could find is: 12factor config approach with Docker

like image 805
rikonor Avatar asked Dec 20 '14 18:12

rikonor


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' ...

Does docker use .env file?

env in your project, it's only used to put values into the docker-compose. yml file which is in the same folder. Those are used with Docker Compose and Docker Stack.


1 Answers

  • Ceate a .env file
 example: test=123 val=Guru
  • Execute command

docker run -it --env-file=.env bash

  • Inside the bash verify using

echo $test (should print 123)

like image 153
Gurudath BN Avatar answered Nov 16 '22 02:11

Gurudath BN