且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Docker echo环境变量

更新时间:2022-03-23 10:19:11

这是预期的行为,看起来很奇怪!

This is the expected behavior, as weird as it seems!

ENTRYPOINT 是列表时(如 ENTRYPOINT [ echo, $ USER] ),按原样使用,无需进一步解析或解释。因此, $ USER 仍然是 $ USER ,因为在此过程中没有涉及用Shell值替换它的shell。 USER 环境变量。

When ENTRYPOINT is a list (as in ENTRYPOINT ["echo", "$USER"]), it is used as-is, without further parsing or interpretation. So $USER remains $USER, because there is no shell involved in the process to replace it with the value of the USER environment variable.

现在,当 ENTRYPOINT 为一个字符串(如 ENTRYPOINT echo $ USER ),实际执行的是 sh -c echo $ USER ,并且 $ USER 被环境变量的值替换(如您所愿)。

Now, when ENTRYPOINT is a string (as in ENTRYPOINT echo $USER), what is actually executed is sh -c "echo $USER", and $USER is replaced with the value of the environment variable (as you would expect).

但是,默认情况下未设置环境变量 USER 。它由登录过程设置;而当您只运行 sh -c ... 时,不涉及登录过程。

However, the environment variable USER is not set by default. It is set by the login process; and when you just run sh -c ... the login process is not involved.

比较环境时运行 docker run -t -i ubuntu bash docker run -t -i ubuntu login -f root 。在前一种情况下,您将获得一个非常基本的环境。在后一种情况下,您将获得习惯的完整环境(包括 USER 变量)。

Compare the environment when running docker run -t -i ubuntu bash and docker run -t -i ubuntu login -f root. In the former case, you will get a very basic environment; in the latter case, you will get the complete environment that you are used to (including USERvariable).