I have this command:
docker run -e "DB_HOST=thehost" --rm my_application echo $DB_HOST
but it shows nothing. I was expecting "thehost" to be shown.
I have tried with simple quotes, double quotes and without quotes.
What am I missing? Do I need to specify ENV parameter in my_aplication's Dockerfile?
If I do:
docker run -e "DB_HOST=thehost" --rm my_application echo $PATH
It shows the PATH value properly. But it is ignoring my env var.
There are a couple of layers of things here:
- Your local shell expands the command;
- Which launches some Docker container;
- Which runs some other process (but not necessarily a shell).
In your first example
docker run ... echo $DB_HOST
your local shell catches the variable reference before it ever gets passed on to Docker.
If you explicitly single-quote it
docker run ... echo '$DB_HOST'
Docker will find /bin/echo
(assuming it exists in the container) and launch that with the string $DB_HOST
as an argument, but again, since no shell is involved on the Docker side, it dutifully prints out that string as is.
The immediate answer to your question is to force there to be a shell on the Docker side
docker run -e DB_HOST=thehost --rm my_application \
sh -c 'echo $DB_HOST'
At a slightly higher level:
- If you're running a program in some other language and not just a shell command, they'll see the environment normally (Python's
os.environ
, Ruby's ENV
, Node's process.env
, etc.)
- If you have anything even a little complex, writing it into a shell script, COPYing that into an image, and running that is probably more maintainable, and implicitly involves a shell (the first line will say
#!/bin/sh
)
- In your Dockerfile, if you say
CMD some command
, Docker will automatically wrap that in a shell; that is equivalent to CMD ["sh", "-c", "some command"]
- The same is true for
ENTRYPOINT
, but it is probably a bug to use it that way