How to change root shell in Dockerfile

Viewed 44

I am writing a Dockerfile to customize the parrotsec/security container to better suit my preferences, and I'd want zsh as the default shell of root. I tried using

RUN chsh -s /usr/bin/zsh

as well as

RUN usermod --shell /usr/bin/zsh root

but after building the image and running it the shell for the root user remains bash.

2 Answers

I believe you'll need to add an ENTRYPOINT to your Dockerfile. If you run a docker inspect <image> you'll see an Entrypoint entry in the output.

See Docker Spawn a shell as a user instead of root which seems pretty closely related to your situation.

It would be nice if, after a USER command, subsequent RUN commands used the default shell set by the user.

This is similar to #7961, but it's more than simply setting the environment variables - for example after ENV SHELL /bin/bash RUN commands will still use /bin/sh.

There are a couple of ways to get around this now, neither of which are especially nice:

Use the exec form and specify a different shell:

RUN ["/bin/bash", "-c", "cmd"]
Point /bin/sh to /bin/bash and manually source:
RUN rm /bin/sh && ln -sf /bin/bash /bin/sh
RUN source ~/.profile && cmd

Profile has to be sourced, because when bash is called from /bin/sh, it doesn't load the profile. (see man pages) Is there any reason the RUN command couldn't just use the shell set for the active user? (For example, a user created with useradd -s /bin/bash.)

Related