如何从dotnet core 2.2和powershell core创建docker镜像?

时间:2019-04-07 19:25:40

标签: docker .net-core powershell-core

我正在尝试“ dockerize”一个dotnet标准库,现在我正在使用一个简单的docker文件,仅使用dotnet cli命令来构建和打包它。

# Build stage
FROM microsoft/dotnet:2.2-sdk as build
ARG Version
WORKDIR /src
COPY . .

RUN dotnet restore
RUN dotnet build -p:Version=$Version -c Release --no-restore

RUN dotnet pack -p:Version=$Version -c Release --include-symbols --no-restore --no-build -o /src/.artifacts

我希望能够从我的docker文件运行powershell脚本ci.ps1,但是dotnet:2.2-sdk中没有安装powershell核心。

是否有关于如何从dotnet映像内部运行powershell核心脚本的示例?如何从Powershell核心和dotnet sdk创建自己的映像?

谢谢

2 个答案:

答案 0 :(得分:0)

dotnet全局工具提供了Powershell支持,可以用作pwsh。

FROM mcr.microsoft.com/dotnet/core/sdk:2.2
USER ContainerAdministrator
WORKDIR /app
RUN dotnet tool install --global PowerShell
RUN setx /M PATH "%PATH%;C:\Users\ContainerUser\.dotnet\tools"
COPY ["publish/"," ."]
ENTRYPOINT ["C:\\Users\\ContainerAdministrator\\.dotnet\\tools\\pwsh.exe", "C:\\app\\PowershellScript.ps1"]

即使在设置路径之后,我也无法直接使用pwsh(powershell),但是进入已安装的路径..it之后,它将提供powershell功能

答案 1 :(得分:0)

这是我最近需要实现的确切用例,您确实可以从.NET Core SDK映像启动并运行Powershell Core-在我的情况下,以下操作针对映像mcr.microsoft.com/dotnet/core/sdk:2.2-stretch

完整的解决方案如下所示:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
...

FROM build AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN dotnet tool install --global PowerShell \
&& ln -s /root/.dotnet/tools/pwsh /usr/bin/pwsh

位置:

  • dotnet tool install --global PowerShell将PowerShell安装到容器中。但是,仅凭此功能还不够好,因为在@ amirtharaj-rs答案中观察到,除非从PowerShell安装目录中调用命令pwsh,否则它将无法工作。 例如,您可能会遇到类似的情况:
root@a1d30119664d:/app# pwsh 
bash: pwsh: command not found
  • ln -s /root/.dotnet/tools/pwsh /usr/bin/pwsh创建一个名为pwsh的链接,该链接指向PowerShell的安装位置。这是可以从任何地方调用pwsh的关键行:
root@a1d30119664d:/app# pwsh
PowerShell 6.2.4
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/pscore6-docs
Type 'help' to get help.

如果上述方法不起作用,则可能需要确保在/usr/bin/pwsh上设置了执行位。您可以通过运行ls -l /usr/bin/pwsh进行检查。

如果确实需要设置执行位,请将chmod 755 /root/.dotnet/tools/pwsh添加到Dockerfile RUN命令中。因此,完整的RUN命令如下所示:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
...

FROM build AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN dotnet tool install --global PowerShell \
&& ln -s /root/.dotnet/tools/pwsh /usr/bin/pwsh
&& chmod 755 /root/.dotnet/tools/pwsh 

如果有兴趣,我通过有关Github问题https://github.com/Microsoft/azure-pipelines-agent/issues/1862的讨论历史了解了以上内容。

相关问题