1. SHELL
1.1. 指令
SHELL ["executable", "parameters"]
1.2. 描述
SHELL指令可以指定新的系统shell来执行 Dockerfile 中的内容。在默认情况下,Linux的默认shell是 [“/bin/sh”, “-c”] ,Windows的是 [“cmd”, “/S”, “/C”] 。
Windows系统中常用cmd和powershell,也可能会同时有 sh ,在构建镜像时可能会比较多的使用到 SHELL 。
SHELL指令必须以JSON格式编写。主要影响的 Dockerfile指令是:RUN、CMD、ENTRYPOINT
SHELL指令可以出现多次,每个SHELL指令覆盖之前的SHELL指令设置的shell,并影响后续的指令。
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
1.3. Windows镜像示例
由于Windows系统的镜像中,默认SHELL指令为SHELL ["cmd", "/S"", "/C"] 所以:
RUN powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"
默认等价于
cmd /S /C powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"
使用JOSON格式
RUN ["powershell", "-command", "Execute-MyCmdlet", "-param1 \"c:\\foo.txt\""]
才默认等价于
powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"
只有在重新定义SHELL如下:
SHELL ["powershell", "-command"]
RUN Execute-MyCmdlet -param1 "c:\foo.txt"
才等价于
powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"