What is the SHELL instruction good for in Docker?
Experience Level: Senior
Tags: Docker
Answer
The SHELL instruction allows the default shell used for the shell form of commands to be overridden. The default shell on Linux is ["/bin/sh", "-c"], and on Windows is ["cmd", "/S", "/C"]. The SHELL instruction must be written in JSON form in a Dockerfile.
The SHELL instruction is particularly useful on Windows where there are two commonly used and quite different native shells: cmd and PowerShell, as well as alternate shells available including sh.
The SHELL instruction can appear multiple times. Each SHELL instruction overrides all previous SHELL instructions, and affects all subsequent instructions.
Example:
FROM microsoft/windowsservercore
# Executed as cmd /S /C echo default
RUN echo default
# Executed as cmd /S /C powershell -command Write-Host default
RUN powershell -command Write-Host default
# Executed as powershell -command Write-Host hello
SHELL ["powershell", "-command"]
RUN Write-Host hello
# Executed as cmd /S /C echo hello
SHELL ["cmd", "/S", "/C"]
RUN echo hello
Related Docker job interview questions
What is the ADD instruction good for in Docker?
Docker SeniorWhat is the STOPSIGNAL instruction good for in Docker?
Docker SeniorHow do ENTRYPOINT and CMD instructions interact?
Docker SeniorHow many CMD instructions can be in a Dockerfile?
Docker SeniorWhat is the CMD instruction good for in Docker?
Docker Senior