11

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

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think that's the reason; it's actually the host expanding the variable before it's sent to docker i.e. FOO="bar" && docker run ubuntu echo $FOO prints bar
The second part of the question escapes the variable as \$FOO, which prevents expansion and results in $FOO being printed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.