首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Docker容器中运行Azure PowerShell脚本

在Docker容器中运行Azure PowerShell脚本
EN

Stack Overflow用户
提问于 2020-11-13 10:20:53
回答 1查看 1.2K关注 0票数 1

我想在Docker容器中运行Azure-PowerShell脚本。由于Docker对我来说是很新的,也许我只是不正确地理解一些概念。

据我所知,下面的Dockerfile是基于Linux的,并且安装了PowerShell和Azure PowerShell吗?

代码语言:javascript
复制
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.

我尝试了各种修改,但目前我得到了以下错误:

代码语言:javascript
复制
container_linux.go:349: starting container process caused "exec: \"cmd\": executable file not found in $PATH"

但是,在“容器”中,它正在工作,但在这里,我无法成功地安装Az PowerShell模块。我也尝试了很多修改,但基本上:

Dockerfile:

代码语言:javascript
复制
## 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更新到version 7):

代码语言:javascript
复制
Exception calling "ShouldContinue" with "2" argument(s): "Object reference not set to an instance of an object." At...\PowerShellGet\1.0.0.1\...

有什么想法吗?提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-14 13:01:05

对于Linux容器,问题在于您试图启动cmd.exe,它是Windows命令行解释器,在Linux中不存在。尝试使用pwsh代替(没有选项)。

因此,SHELL行应该如下所示:

代码语言:javascript
复制
SHELL ["pwsh"]

而不是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64819185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档