运行 Docker 容器中的 Azure PowerShell 脚本
Run Azure PowerShell script in Docker container
我想在 Docker 容器中 运行 一个 Azure-PowerShell 脚本。由于 Docker 对我来说很新,可能我只是没有正确理解一些概念。
据我了解,下面的 Docker 文件基于 Linux,并且具有 PowerShell 和 Azure PowerShell 安装了吗?
FROM mcr.microsoft.com/azure-powershell:latest
COPY . /app
SHELL ["cmd", "/S", "/C"]
RUN "& ./app/WriteHostTest.ps1" #For testing purposes I just want to fire a really simple .ps1 file.
我尝试了各种修改,但目前出现此错误:
container_linux.go:349: starting container process caused "exec: \"cmd\": executable file not found in $PATH"
在“Windows 容器” 中它正在运行,但是,在这里我无法安装 Az PowerShell 模块。我也尝试了很多修改,但基本上:
Docker文件:
## Create first layer: Windows image
FROM mcr.microsoft.com/windows/servercore:ltsc2019
LABEL Robin Bette
## Create second layer: copy everything from the CURRENT DIRECTORY to /app location
COPY . /app
## Download the tools
SHELL ["cmd", "/S", "/C"]
ADD "https://dist.nuget.org/win-x86-commandline/v5.8.0/nuget.exe" "C:\TEMP\nuget.exe"
## Install NuGet
RUN "C:\TEMP\nuget.exe" install NuGet.Build -Version 2.12.1
## Install PowerShellGet
SHELL ["powershell", "-ExecutionPolicy", "Bypass", "-Command"]
RUN Install-Module -Name PowerShellGet -RequiredVersion 2.2.1 -Force
# Install Azure PowerShell
RUN Install-Module -Name Az -Force
## Run the PowerShell script
RUN PowerShell ./app/Az-Script.ps1
## Stuff I tried:
# Install PS7 (did not help)
# RUN iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI" => Error on the ampersand
#ADD "https://github.com/PowerShell/PowerShell/releases/download/v7.1.0/PowerShell-7.1.0-win-x64.msi" "C:\TEMP\PowerShell-7.1.0-win-x64.msi"
#RUN "C:\TEMP\PowerShell-7.1.0-win-x64.msi"
#RUN Install-PackageProvider -Name NuGet -Force
#RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://aka.ms/install-powershell.ps1'))
Install-PackageProvider : No match was found for the specified search criteria
for the provider 'NuGet'. The package provider requires 'PackageManagement'
and 'Provider' tags. Please check if the specified package has the tags.
Exception calling "ShouldContinue" with "2" argument(s): "Object reference not set to an instance of an object."
在这里,我一直收到错误(为此我也尝试了各种解决方案,例如将 PowerShell 更新到版本 7):
Exception calling "ShouldContinue" with "2" argument(s): "Object reference not set to an instance of an object." At...\PowerShellGet.0.0.1\...
有什么想法吗?提前致谢!
对于 Linux 容器,问题是您正在尝试启动 cmd.exe,它是 Windows 命令行解释器,在 [=19= 中不存在].尝试改用 pwsh(不带选项)。
所以 SHELL 行应该类似于:
SHELL ["pwsh"]
相反。
我想在 Docker 容器中 运行 一个 Azure-PowerShell 脚本。由于 Docker 对我来说很新,可能我只是没有正确理解一些概念。
据我了解,下面的 Docker 文件基于 Linux,并且具有 PowerShell 和 Azure PowerShell 安装了吗?
FROM mcr.microsoft.com/azure-powershell:latest
COPY . /app
SHELL ["cmd", "/S", "/C"]
RUN "& ./app/WriteHostTest.ps1" #For testing purposes I just want to fire a really simple .ps1 file.
我尝试了各种修改,但目前出现此错误:
container_linux.go:349: starting container process caused "exec: \"cmd\": executable file not found in $PATH"
在“Windows 容器” 中它正在运行,但是,在这里我无法安装 Az PowerShell 模块。我也尝试了很多修改,但基本上:
Docker文件:
## Create first layer: Windows image
FROM mcr.microsoft.com/windows/servercore:ltsc2019
LABEL Robin Bette
## Create second layer: copy everything from the CURRENT DIRECTORY to /app location
COPY . /app
## Download the tools
SHELL ["cmd", "/S", "/C"]
ADD "https://dist.nuget.org/win-x86-commandline/v5.8.0/nuget.exe" "C:\TEMP\nuget.exe"
## Install NuGet
RUN "C:\TEMP\nuget.exe" install NuGet.Build -Version 2.12.1
## Install PowerShellGet
SHELL ["powershell", "-ExecutionPolicy", "Bypass", "-Command"]
RUN Install-Module -Name PowerShellGet -RequiredVersion 2.2.1 -Force
# Install Azure PowerShell
RUN Install-Module -Name Az -Force
## Run the PowerShell script
RUN PowerShell ./app/Az-Script.ps1
## Stuff I tried:
# Install PS7 (did not help)
# RUN iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI" => Error on the ampersand
#ADD "https://github.com/PowerShell/PowerShell/releases/download/v7.1.0/PowerShell-7.1.0-win-x64.msi" "C:\TEMP\PowerShell-7.1.0-win-x64.msi"
#RUN "C:\TEMP\PowerShell-7.1.0-win-x64.msi"
#RUN Install-PackageProvider -Name NuGet -Force
#RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://aka.ms/install-powershell.ps1'))
Install-PackageProvider : No match was found for the specified search criteria
for the provider 'NuGet'. The package provider requires 'PackageManagement'
and 'Provider' tags. Please check if the specified package has the tags.
Exception calling "ShouldContinue" with "2" argument(s): "Object reference not set to an instance of an object."
在这里,我一直收到错误(为此我也尝试了各种解决方案,例如将 PowerShell 更新到版本 7):
Exception calling "ShouldContinue" with "2" argument(s): "Object reference not set to an instance of an object." At...\PowerShellGet.0.0.1\...
有什么想法吗?提前致谢!
对于 Linux 容器,问题是您正在尝试启动 cmd.exe,它是 Windows 命令行解释器,在 [=19= 中不存在].尝试改用 pwsh(不带选项)。
所以 SHELL 行应该类似于:
SHELL ["pwsh"]
相反。