I want the default command for a Docker image to start up a login shell for the container's user.¹
Something like:
…
USER someuser
CMD /bin/zsh --login
That above gets a login shell, but does not change the working directory to someuser's home, nor does it honor someuser's /etc/passwd entry. sudo --user someuser --login will achieve the desired result (correct shell, working directory as user's home), but that depends on someuser being properly provisioned in /etc/sudoers and leaves the parent sudo process hanging around. I don't want to rely on sudo at all, if I don't have to.
I have tried /usr/bin/login -p -f someuser as the command, but this doesn't seem to work (not sure what the error is).
One can try something like CMD sh -c 'cd "${HOME}" ; SHELL="${SHELL}" exec -a "-${SHELL##*/}" "${SHELL}"' (derived from a discussion with a different, but related context). This seems to work, but that depends on SHELL being set and the referenced shell inferring that it should start as a login shell when it's zeroth argument starts with a -. Is this idiomatic? I don't know if SHELL is always set or whether prefixing the user shell with - will always work. (Note that CMD sh -c '… exec -l "${SHELL}"' doesn't get it quite right, because the zeroth argument gets set to, e.g., -/usr/bin/zsh.)
This seems to suggest agetty can create the desired effect, but seems like a mismatch on how to get there. I also don't want to engage in my own grep/awk shenanigans with /etc/passwd (although getent passwd "$( id -u )" | cut -d : -f 7 seems to be the least obnoxious of these approaches).
Is there a way a user can start its own login shell as if the user were logging in without naming the shell or the home directory explicitly and without a password?
There has to be something more idiomatic than exec python -c 'import os, pwd, re ; ent = pwd.getpwuid(os.getuid()) ; os.chdir(ent.pw_dir) ; os.execv(ent.pw_shell, (re.sub(r"^.*/", "-", ent.pw_shell),))'.
¹ The image is meant to create a persistent, named container that houses a sandboxed, interactive environment for experimentation with a specific set of pre-installed applications. (This isn't some containerized web application. Think of running a highly customized suite of Linux-only math/science apps on a Windows host with minimal required configuration.) Power users may want to change the login shell for someuser inside the container via chsh, and that should be honored by the default command.