Docker为什么未设置$ USER环境变量 [英] Docker why isn't $USER environment variable set

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

问题描述

我正在做一个简单的docker build,其中映像运行一个脚本,该脚本假定已设置 $ USER ,就像通常在bash shell中一样.

I'm doing a simple docker build where the image runs a script that assumes $USER is set, like it normally is in a bash shell.

但是,当使用/bin/bash /bin/bash --login 时,未设置 $ USER .使用最新的 ubuntu

However, $USER is not set, when using /bin/bash or /bin/bash --login. Very easy to demonstrate using the latest ubuntu

$ docker run -t -i ubuntu:latest
root@1dbeaefd6cd4:/# echo $USER

root@1dbeaefd6cd4:/# exit

$ docker run -t -i ubuntu:latest /bin/bash --login
root@d2728a8188a5:/# echo $USER

root@1dbeaefd6cd4:/# exit

但是,如果在外壳程序中我 su -l root ,则设置了 $ USER .

However, if in the shell I su -l root, then $USER is set.

root@d2728a8188a5:/# su -l root
root@d2728a8188a5:~# echo $USER
root
root@d2728a8188a5:~# exit

我知道我可以在 Dockerfile 中添加 ENV USER = root ,但是我试图避免对值进行硬编码.

I'm aware I could add ENV USER=root to the Dockerfile, but I'm trying to avoid hard-coding the value.

有人建议为什么会发生这种情况吗?我主要是出于好奇,要求了解Docker启动 bash 时发生的情况.显然,它与登录shell并不完全一样,-login 选项似乎不起作用.

Does anyone have a suggestion of why this might be happening? I'm asking mostly out of curiosity to understand what's happening when Docker starts bash. It's clearly not exactly like a login shell and the --login option doesn't seem to be working.

推荐答案

The only environment variables documented to be set are $HOME, $HOSTNAME, $PATH, and (maybe) $TERM. (A docker build RUN step internally does the equivalent of docker run.) If you need other variables set you can use the ENV directive.

通常在Dockerfile中,不需要特别配置路径名或用户名,因为它们位于与主机分离的隔离空间中.例如,即使不是标准的FHS路径,将应用程序"放在/app 中也是非常普遍的.尽管不经常这样做,但最好的做法是设置一些非root用户来实际运行应用程序,并在Dockerfile的后期使用 USER 指令.在所有这些情况下,您都知道用户名实际上是什么;这不是任何参数.

Typically in a Dockerfile there's no particular need to make path names or user names configurable, since these live in an isolated space separate from the host. For instance, it's extremely common to put "the application" in /app even though that's not a standard FHS path. It's considered a best practice, though infrequently done, to set up some non-root user to actually run the application and use a USER directive late in a Dockerfile. In all of these cases you know what the username actually is; it's not any sort of parameter.

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

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