Docker echo环境变量 [英] Docker echo environment variable

查看:308
本文介绍了Docker echo环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小的docker文件,该文件设置了一个User,只是回显了当前用户,作为一个小例子来向自己证明它正在运行。我尝试了许多变体,但在文档中找不到太多帮助。

I'm trying to write a little docker file that sets a User and just echos the current user as a little example to prove to myself it is working. I've tried a number of variants and couldn't find much help in the documentation.

FROM ubuntu
USER daemon
# ENTRYPOINT ["echo", "$USER"]
# just gives "$USER"
# ENTRYPOINT ["echo", "-e", "${USER}"]
# just gives "$USER"
# ENTRYPOINT echo $USER
# gives empty string
# ENTRYPOINT ["/bin/echo", "$USER"]
# just gives "$USER"

我正在运行 docker build 。在dockerfile上,然后运行 docker run< image-id> 并获取结果

I'm running docker build . on the dockerfile and then running docker run <image-id> and getting the results

预期结果是守护程序,或者没有 USER守护程序行,我希望

Expected result is daemon, or without the USER daemon line, I expect root. Probably a really simple answer.

推荐答案

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

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).

这篇关于Docker echo环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆