Dockerfile运行管道powershell



My Dockerfile包含以下行

RUN pwsh -Command Find-Module deployment.aws.applications -AllVersions | Where-Object {[version]$_.Version -ge '5.1.10' -and [version]$_.Version -le '100.0.0'} | ForEach-Object { Install-Module -Name $_.Name -RequiredVersion $_.Version -Repository  ${REPOSITORY} -Scope AllUsers -AcceptLicense -Force } 

哪个输出:

=> [2/2] RUN pwsh -Command Find-Module deployment.aws.applications -AllVersions | Where-Object {[version]$_.Version -ge '5.1.10' -and [version]$_.Version -le '100.0.0'} | ForEach-Object { Install-Module -Name $_.Name -Required  14.0s
=> => # /bin/sh: 1: ForEach-Object: not found
=> => # /bin/sh: 1: Where-Object: not found

看起来正在发生的情况是,管道(|(没有被视为Powershell命令。我把整行都用引号括起来了,但Dockerfile参数没有得到解决。

为了使整个问题更加复杂,我想通过一个Dockerfile run命令运行多个Powershell命令。像这样:

RUN pwsh -Command Find-Module deployment.aws.applications -AllVersions | Where-Object {[version]$_.Version -ge '5.1.10' -and [version]$_.Version -le '100.0.0'} | ForEach-Object { Install-Module -Name $_.Name -RequiredVersion $_.Version -Repository  ${REPOSITORY} -Scope AllUsers -AcceptLicense -Force }  && 
pwsh -Command Find-Module x.y.z -AllVersions | Where-Object {[version]$_.Version -ge '5.1.10' -and [version]$_.Version -le '100.0.0'} | ForEach-Object { Install-Module -Name $_.Name -RequiredVersion $_.Version -Repository  ${REPOSITORY} -Scope AllUsers -AcceptLicense -Force }

如果Powershell命令变得越来越复杂,那么将它们放入脚本中并使用RUN执行该脚本会更容易。

查找安装模块.ps1

Find-Module deployment.aws.applications -AllVersions | Where-Object {[version]$_.Version -ge '5.1.10' -and [version]$_.Version -le '100.0.0'} | ForEach-Object { Install-Module -Name $_.Name -RequiredVersion $_.Version -Repository  ${REPOSITORY} -Scope AllUsers -AcceptLicense -Force } 

Dockerfile

ADD Find-Install-Modules.ps1 .
RUN pwsh -File Find-Install-Modules.ps1

最新更新