如何在 aspnet Nanoserver docker 容器中安装 Powershell Core?

2024-01-08

是否有可能以某种方式将 Powershell Core 添加到 AspNet Nano Server 基础映像中?

我们使用多级 dockerfile 来生成包含 .net core Web 应用程序的映像。最终映像基于 mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1809,添加了我们的 .net core 二进制文件(为了将映像大小保持在最小)。

我们希望将 Powershell Core 包含在最终映像中,以用于初始化和调试目的,但到目前为止尚未找到方法。我们考虑了各种命令行安装选项(msiexec、choco 和谷歌搜索了很多),但没有找到解决方案。

很感谢任何形式的帮助!


From 微软 https://learn.microsoft.com/en-us/windows-server/get-started/nano-in-semi-annual-channel:

默认情况下不再包含 PowerShell Core、.NET Core 和 WMI,但您可以在构建容器时包含 PowerShell Core 和 .NET Core 容器包。

虽然没有官方powershell core支持aspnet:3.1-nanoserver-1809,有一个官方powershell core支持纯nanoserver-18.09, see this https://hub.docker.com/_/microsoft-powershell带有图像标签:lts-nanoserver-1809

所以,您需要做的最后一件事是:

只需按照微软的建议即可include powershell core when build your own image.

接下来是Dockerfile https://github.com/PowerShell/PowerShell-Docker/blob/master/release/lts/nanoserver1809/docker/Dockerfile如何nanoserver-18.09包括powershell core,你可以复制这个来编写你自己的dockerfile来添加powershell core给你的aspnet:3.1-nanoserver-1809:

# escape=`
# Args used by from statements must be defined here:
ARG InstallerVersion=nanoserver
ARG InstallerRepo=mcr.microsoft.com/powershell
ARG NanoServerRepo=mcr.microsoft.com/windows/nanoserver

# Use server core as an installer container to extract PowerShell,
# As this is a multi-stage build, this stage will eventually be thrown away
FROM ${InstallerRepo}:$InstallerVersion  AS installer-env

# Arguments for installing PowerShell, must be defined in the container they are used
ARG PS_VERSION=7.0.0-rc.1

ARG PS_PACKAGE_URL=https://github.com/PowerShell/PowerShell/releases/download/v$PS_VERSION/PowerShell-$PS_VERSION-win-x64.zip

# disable telemetry
ENV POWERSHELL_TELEMETRY_OPTOUT="1"

SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

ARG PS_PACKAGE_URL_BASE64

RUN Write-host "Verifying valid Version..."; `
    if (!($env:PS_VERSION -match '^\d+\.\d+\.\d+(-\w+(\.\d+)?)?$' )) { `
        throw ('PS_Version ({0}) must match the regex "^\d+\.\d+\.\d+(-\w+(\.\d+)?)?$"' -f $env:PS_VERSION) `
    } `
    $ProgressPreference = 'SilentlyContinue'; `
    if($env:PS_PACKAGE_URL_BASE64){ `
        Write-host "decoding: $env:PS_PACKAGE_URL_BASE64" ;`
        $url = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($env:PS_PACKAGE_URL_BASE64)) `
    } else { `
        Write-host "using url: $env:PS_PACKAGE_URL" ;`
        $url = $env:PS_PACKAGE_URL `
    } `
    Write-host "downloading: $url"; `
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12; `
    New-Item -ItemType Directory /installer > $null ; `
    Invoke-WebRequest -Uri $url -outfile /installer/powershell.zip -verbose; `
    Expand-Archive /installer/powershell.zip -DestinationPath \PowerShell

# Install PowerShell into NanoServer
FROM ${NanoServerRepo}:1809

ARG IMAGE_NAME=mcr.microsoft.com/powershell

# Copy PowerShell Core from the installer container
ENV ProgramFiles="C:\Program Files" `
    # set a fixed location for the Module analysis cache
    PSModuleAnalysisCachePath="C:\Users\Public\AppData\Local\Microsoft\Windows\PowerShell\docker\ModuleAnalysisCache" `
    # Persist %PSCORE% ENV variable for user convenience
    PSCORE="$ProgramFiles\PowerShell\pwsh.exe" `
    # Set the default windows path so we can use it
    WindowsPATH="C:\Windows\system32;C:\Windows" `
    POWERSHELL_DISTRIBUTION_CHANNEL="PSDocker-NanoServer-1809"

### Begin workaround ###
# Note that changing user on nanoserver is not recommended
# See, https://learn.microsoft.com/en-us/virtualization/windowscontainers/manage-containers/container-base-images#base-image-differences
# But we are working around a bug introduced in the nanoserver image introduced in 1809
# Without this, PowerShell Direct will fail
# this command sholud be like this: https://github.com/PowerShell/PowerShell-Docker/blob/f81009c42c96af46aef81eb1515efae0ef29ad5f/release/preview/nanoserver/docker/Dockerfile#L76
USER ContainerAdministrator

# This is basically the correct code except for the /M
RUN setx PATH "%PATH%;%ProgramFiles%\PowerShell;" /M

USER ContainerUser
### End workaround ###

COPY --from=installer-env ["\\PowerShell\\", "$ProgramFiles\\PowerShell"]

# intialize powershell module cache
RUN pwsh `
        -NoLogo `
        -NoProfile `
        -Command " `
          $stopTime = (get-date).AddMinutes(15); `
          $ErrorActionPreference = 'Stop' ; `
          $ProgressPreference = 'SilentlyContinue' ; `
          while(!(Test-Path -Path $env:PSModuleAnalysisCachePath)) {  `
            Write-Host "'Waiting for $env:PSModuleAnalysisCachePath'" ; `
            if((get-date) -gt $stopTime) { throw 'timout expired'} `
            Start-Sleep -Seconds 6 ; `
          }"

# re-enable telemetry
ENV POWERSHELL_TELEMETRY_OPTOUT="0"

CMD ["pwsh.exe"]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 aspnet Nanoserver docker 容器中安装 Powershell Core? 的相关文章

随机推荐