What is SHELL in Dockerfile: Explanation and Usage
SHELL instruction in a Dockerfile sets the default shell used to run commands like RUN, CMD, and ENTRYPOINT. It lets you specify which shell and options to use, overriding the default shell (usually /bin/sh -c).How It Works
The SHELL instruction changes the shell environment that Docker uses to run commands inside the container during build or runtime. By default, Docker uses /bin/sh -c on Linux, which is like running commands in a simple command prompt.
Think of it like choosing which kitchen you want to cook in. Normally, Docker uses a basic kitchen (/bin/sh), but with SHELL, you can pick a different kitchen, like /bin/bash, which might have more tools or features. This helps when your commands need special shell features or syntax.
Once you set SHELL, all following commands in the Dockerfile use that shell until you change it again or the build ends.
Example
This example shows how to use SHELL to switch to /bin/bash with the -c option, allowing you to use bash-specific features in your commands.
FROM ubuntu:22.04 # Change default shell to bash SHELL ["/bin/bash", "-c"] # Use bash-specific syntax (brace expansion) RUN echo {1..5} CMD ["bash"]
When to Use
Use SHELL when your Dockerfile commands need a different shell than the default /bin/sh. For example, if you want to use bash features like brace expansion, arrays, or advanced scripting, switching to /bin/bash helps.
It is also useful when working on Windows containers where PowerShell is preferred, or when you want to add shell options like -e to stop on errors.
In real life, if you are writing complex scripts inside your Dockerfile or need consistent shell behavior across different base images, SHELL lets you control the environment easily.
Key Points
- SHELL sets the default shell for commands in the Dockerfile.
- It overrides the default
/bin/sh -cshell on Linux containers. - Useful for enabling shell features not available in the default shell.
- Applies to all following commands until changed again.
- Supports specifying shell and its options as a JSON array.