damon
damon

Reputation: 15128

How can I use environment variables in a docker run command?

If I pass an environment variable which is set inside of 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 these will work:

# Prints blank line
$ docker run -e FOO=bar ubuntu echo $FOO

# Prints '$FOO'
$ docker run -r FOO=bar ubuntu echo \$FOO

Upvotes: 11

Views: 9444

Answers (1)

z0r
z0r

Reputation: 8595

It works if you run echo in a 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's the shell that does the variable substitution.

Upvotes: 12

Related Questions