简体   繁体   中英

docker ubuntu container: shell linked to bash still starts shell

Alright guys, so I try to install rvm in a docker container based on ubuntu:14.04. During the process, I discovered that some people do something like this to ensure docker commands are also run with the bash:

RUN ln -fs /bin/bash /bin/sh

Now The weirdness happens and I hope someone of you can explain it to me:

→ docker run -it --rm d81ff50de1ce /bin/bash
root@e93a877ab3dc:/# ls -lah /bin
....
lrwxrwxrwx  1 root root    9 Mar  1 16:15 sh -> /bin/bash
lrwxrwxrwx  1 root root    9 Mar  1 16:15 sh.distrib -> /bin/bash
...
root@e93a877ab3dc:/# /bin/sh
sh-4.3# echo $0
/bin/sh

Can someone explain what's going on here? I know I could just prefix my commands in the dockerfile w/ bash -c , but I would like to understand what is happening here and if possible still ditch the bash -c prefix in the dockerfile.

Thanks a lot, Robin

It's because bash has a compatibility mode where it tries to emulate sh if it is started via the name sh , as the manpage says:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files. When invoked as sh, bash enters posix mode after the startup files are read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM