ealeon
ealeon

Reputation: 12460

Docker: not being able to set env variable

Nothing shows up for env var FOO even though I've set it:

$ docker run -e FOO=foofoo ubuntubr echo $PATH
/bin:/usr/ucb:/usr/bin:/usr/sbin:/sbin:/usr/etc:/etc
$ docker run -e FOO=foofoo ubuntubr echo $FOO
$ 

What did I do wrong?

But I was able to modify the path:

docker run -e PATH=/nopath ubuntubr echo $PATH
docker: Error response from daemon: OCI runtime create failed: container_linux.go:296: starting container process caused "exec: \"echo\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled 

Then why isn't docker run -e FOO=foofoo ubuntubr echo $FOO isn't printing foofoo?

Upvotes: 2

Views: 1369

Answers (1)

tgogos
tgogos

Reputation: 25250

The variables are set. The way you are trying to verify that they are set is wrong.

It's called "variable expansion" and I quote from the answer @larsks has given here:

$ docker run -it alpine echo $HOME
/home/lars

$ docker run -it alpine echo '$HOME'
$HOME

$ docker run -it alpine sh -c 'echo $HOME'
/root
  1. it prints the $HOME variable of your host, not the container's. In your case $FOO doesn't exit on the host, so it prints an empty line
  2. it prints $HOME (like a string)
  3. this works, and prints the $HOME variable of the container

Example for your case:

$ docker run -e FOO=foofoo alpine sh -c 'echo $FOO'
foofoo

Thanks @vmonteco for pointing out docker inspect for debuging. If you want to learn more on what your containers were actually doing, below are the CMD set for the 3 different cases discussed previously:

  1. "Cmd": ["echo"]
  2. "Cmd": ["echo","$FOO"]
  3. "Cmd": ["sh","-c","echo $FOO"]

Upvotes: 5

Related Questions