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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With