What You'll Learn
You'll learn how to use Docker's SHELL instruction to change which shell interprets your RUN, CMD, and ENTRYPOINT commands written in shell form.
Understanding the Default Shell
By default, Docker executes shell-form commands using /bin/sh -c on Linux and cmd /S /C on Windows. Think of this as the "language" Docker speaks when running your commands. Sometimes you need a different shell—perhaps you want bash for its advanced features, or powershell on Windows for better scripting.
The SHELL instruction lets you change this default interpreter for all subsequent instructions in your Dockerfile.
How SHELL Works
# Default behavior (implicit)
RUN echo "Hello"
# Runs as: /bin/sh -c 'echo "Hello"'
# Change the shell
SHELL ["/bin/bash", "-c"]
# Now this uses bash
RUN echo "Hello from ${BASH_VERSION}"
# Runs as: /bin/bash -c 'echo "Hello from ${BASH_VERSION}"'
The SHELL instruction takes a JSON array format—the first element is the shell executable, followed by any flags. Once set, it affects all RUN, CMD, and ENTRYPOINT instructions written in shell form that come after it.
Why This Matters
Different shells have different capabilities. Bash offers features like arrays and pattern matching that /bin/sh lacks. On Windows, PowerShell provides better error handling than cmd. By explicitly setting the shell, you ensure your commands run in an environment that supports the syntax you're using.
You can use SHELL multiple times in a Dockerfile if different sections need different shells.
Key Takeaway: The SHELL instruction changes the default shell for executing subsequent shell-form commands, giving you control over the command interpreter and access to shell-specific features.