brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the SHELL instruction changes which shell RUN uses in a Dockerfile

Docker
~/tips/tip-dockerfile-shell-instruction

SHELL ["/bin/bash", "-c"]

Overrides the default shell for shell-form RUN, CMD and ENTRYPOINT lines in the rest of the Dockerfile.

Every RUN line in a Dockerfile goes through /bin/sh -c, and sometimes that is not the shell you wanted. The shell decides how pipes and exit codes behave in your build.

SHELL ["/bin/bash", "-c"]
RUN set -o pipefail && wget -O - https://example.com/file | wc -l > /number

The docs say the default shell on Linux is ["/bin/sh", "-c"] and on Windows ["cmd", "/S", "/C"]. SHELL replaces that default for all shell-form instructions after it. Each new SHELL line overrides the previous one, so you can switch back and forth in a single file. The docs show this on Windows, where one stage uses cmd and the next uses powershell -command.

The Docker build docs give a reason to care on Linux. In a pipe, /bin/sh -c only looks at the exit code of the last command, so a failing wget before wc does not fail the build. Prepending set -o pipefail && fixes that, but the docs warn that not every shell supports the option, for example dash on Debian-based images. With SHELL you choose a shell that does, so you do not have to repeat the workaround in every RUN.

One caveat: the shell you name has to exist in the image. A minimal base image may not ship bash, and then the first RUN after SHELL fails. Check what the base image contains before you rely on it.

Further reading: Dockerfile reference: SHELL, Build best practices: pipes

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events docker compose volumes All Docker tips (192) All topics

Link