当前位置: 动力学知识库 > 问答 > 编程问答 >

bash - Access container environment variables in inline run command

问题描述:

The task is very simple, I know an environment variable is (will be) set inside container and I want to use it in my inline run command. However the value does not get populated:

docker run --rm --link other ubuntu printenv | grep OTHER_ENV_VAR_NAME && echo ${OTHER_ENV_VAR_NAME}

current output of above command is:

> OTHER_ENV_VAR_NAME=valueOfVariableInLinkedContainer

>

the output of echo on second line is empty. what I expect to get is:

> OTHER_ENV_VAR_NAME=valueOfVariableInLinkedContainer

> valueOfVariableInLinkedContainer

the printenv gives the right value but the value passed to echo is not correct. I suggest that value is being evaluated on my host and not inside the container. Therefore I tried to use single and double quotes around echo command and to escape the $ using \ with no success.

My host machine is also an Ubuntu.

网友答案:

In order to pipe and echo inside the same CMD of a docker run, use a shell command (sh -c)

docker run --rm --link other ubuntu sh -c 'printenv | grep OTHER_ENV_VAR_NAME && echo ${OTHER_ENV_VAR_NAME}'

If you don't, the pipe grep and the echo would take place in your host shell (the one where docker run is executed).
That would not echo anything, since the environment value is only set within the docker session.

分享给朋友:
您可能感兴趣的文章:
随机阅读: