云计算|云计算核心技术Docker教程:Dockerfile文件SHELL命令详解

来源 : 99API发布 : 2020-11-04 10:59:15访问数 : 2点赞数 : 1

  Dockerfile文件SHELL指令可以覆盖命令的shell模式所使用的默认shell。Linux的默认shell是[“/bin/sh”, “-c”],Windows的是[“cmd”, “/S”, “/C”]。SHELL指令必须以JSON格式编写。

  语法格式

  SHELL ["executable", "parameters"]

  该SHELL指令在Windows上特别有用,在Windows上有两个常用且完全不同的本机shell:cmd和powershell,以及可用的替代shell包括sh。

  该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

  当RUN, CMD和ENTRYPOINT使用shell形式时,将使用SHELL指令设置的shell执行。

  以下的示例是windows常见的模式,可以使用SHELL指令精简:

  RUN powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"

  docker调用的命令将是:

  cmd /S /C powershell -command Execute-MyCmdlet -param1 "c:\foo.txt"

  这效率低下有两个原因。首先,有一个不必要的cmd.exe命令处理器(也称为Shell)被调用。其次,shell 形式的每条RUN指令都需要在命令前加上前缀。powershell -command

  为了使其更有效,可以采用两种机制之一。一种是使用RUN命令的JSON形式,例如:

  RUN ["powershell", "-command", "Execute-MyCmdlet", "-param1 \"c:\\foo.txt\""]

  虽然没有调用了cmd.exe,不过需要对双引号进行转义,看起来比较冗长。另一种机制是使用SHELL指令和shell形式,使得windows用户可以使用更自然的语法,特别是与escape指令一起用时:

  # escape=`

  FROM microsoft/nanoserver

  SHELL ["powershell","-command"]

  RUN New-Item -ItemType Directory C:\Example

  ADD Execute-MyCmdlet.ps1 c:\example\

  RUN c:\example\Execute-MyCmdlet -sample 'hello world'


1

电子邮箱:

service@99api.com

联系电话:

17350028402

微信客服:

电子邮箱:

service@99api.com

联系电话:

17350028402

微信客服: