Skip to content
Advertisement

exporting list of variables in docker run

We have a set of variables in env file as given below

examples.env

A="/path1"
B="/path2":$A

Now, docker run cannot substitute $B for /path/path1, due to its limitations

So, I want to export the variable in launcher script and then call those variable using -e flag, as given below

mydocker.sh

input="examples.env"
while IFS= read -r line
do
  export $line
done < "$input"

docker run --rm -e <Some code> centos8

Now how to create docker command to get all the variables?

Following docker command works docker run --rm -e A -e B centos8 But If the number of variables in examples.env file is unknown, then how can we generate docker run command?

Advertisement

Answer

Source the variables file in your mydocker.sh script insted of export and concat each variable with --env, at the and eval the concatenated string to variable so the variables will interpreted.

Here is an example:

# Source the variables file so they will be available in current script.
. ./examples.env
# Define docker env string it will lokk like below:.
# --env A=/path1 --env B=/path1/path2
dockerenv=""
input="examples.env"
while IFS= read -r line
do
    dockerenv="${dockerenv} --env $line"
done < "$input"
# Evaluate the env string so the variables in it will be interpreted
dockerenv=$(eval echo $dockerenv)

docker run --rm $dockerenv centos8

P.S.
You need the --env insted of -e becouse -e will be interpreted as echo command argument.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement