使用 PowerShell 作为临时命令行管理程序,但在运行时将其丢弃以获取批处理文件



我在Dockerfile中使用PowerShell作为默认外壳来完成一些任务,例如将新用户和组添加到我的容器构建中。

DockerFile (相關)

SHELL ["powershell.exe", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'Continue'; $verbosePreference='Continue';"]
RUN New-LocalUser -Name "testuser" -Password (ConvertTo-SecureString -AsPlainText "CrackPassword!" -Force) -FullName "Test.User" -Description "LocalAdministrator"
RUN Add-LocalGroupMember -Group administrators -Member  testuser -Verbose
RUN "C:/Windows/Microsoft.NET/Framework64/v4.0.30319/InstallUtil.exe" /username=.testuser /password=CrackPassword! /LogToConsole=true /ShowCallStack TestDockerService.exe

在同一DockerFile中,我指定一个CMD在Docker container运行时运行批处理文件。

COPY testbat.bat /testbat.bat
CMD testbat.bat && cmd

图像构建良好,但Docker Run不起作用,抱怨:

At line:1 char:111
+ ... rence = 'Continue'; $verbosePreference='Continue'; testbat.bat && cmd
+                                                                    ~~
The token '&&' is not a valid statement separator in this version.
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : InvalidEndOfLine

我猜在 SHELL 中运行 CMD 存在问题,一些快速研究表明可能是这样......

有没有办法关闭调用的SHELL会话,或者在运行时使用 CMDoverrride它?

尝试根据壳牌的文档在 CMD 之前添加以下内容

SHELL ["cmd", "/S", "/C"]

最新更新