How to use environment variables in docker launch command?

If I pass an environment variable that is set inside the container as an argument to docker run , my shell evaluates it. For example:

I want my container to print the value of $FOO , which is bar . None of them will work:

 # Prints blank line $ docker run -e FOO=bar ubuntu echo $FOO # Prints '$FOO' $ docker run -r FOO=bar ubuntu echo \$FOO 
+8
docker
source share
1 answer

It works if you run echo in the shell:

 $ sudo docker run --rm -e FOO=bar ubuntu bash -c 'echo $FOO' bar 

This is because echo is a command ( /bin/echo ), but it is a wrapper that performs variable replacement.

+7
source share

All Articles