Passing environment variable to container is not w

2019-03-03 06:26发布

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.

1条回答
Summer. ? 凉城
2楼-- · 2019-03-03 06:52

There are a couple of layers of things here:

  1. Your local shell expands the command;
  2. Which launches some Docker container;
  3. 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
查看更多
登录 后发表回答