Docker why isn't $USER environment variable set

喜夏-厌秋 提交于 2020-08-08 05:19:12

问题


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.

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

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

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

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.


回答1:


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.

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.




回答2:


According to https://www.tldp.org/LDP/abs/html/internalvariables.html:

The variables $ENV, $LOGNAME, $MAIL, $TERM, $USER, and $USERNAME are not Bash builtins. These are, however, often set as environmental variables in one of the Bash or login startup files.

Also as pointed out in this Unix&Linux SE answer Who sets $USER and $USERNAME environment variables?:

There's no rule. Some shells like tcsh or zsh set $LOGNAME. zsh sets $USER.

It may be set by some things that log you in like login (as invoked by getty when login on a terminal and sometimes by other things like in.rlogind), cron, su, sudo, sshd, rshd, graphical login managers or may not.

[…]

So in the context of a clean environment within a Docker container, you may rather want to rely on whoami or id:

$ docker run --rm -it ubuntu
root@7f6191875c62:/# whoami
root
root@7f6191875c62:/# id
uid=0(root) gid=0(root) groups=0(root)
root@7f6191875c62:/# id -u -n
root



回答3:


Try running "/bin/bash -i" which will force interactive mode and execute the .profile where environment variables are usually stored.



来源:https://stackoverflow.com/questions/54411218/docker-why-isnt-user-environment-variable-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!