How to install Powershell Core in aspnet Nanoserver docker container? How to install Powershell Core in aspnet Nanoserver docker container? powershell powershell

How to install Powershell Core in aspnet Nanoserver docker container?


From Microsoft:

PowerShell Core, .NET Core, and WMI are no longer included by default, but you can include PowerShell Core and .NET Core container packages when building your container.

Although there is no official powershell core support for aspnet:3.1-nanoserver-1809, there is an official powershell core support for pure nanoserver-18.09, see this with the image tag: lts-nanoserver-1809

So, the last thing you need to do is:

Just follow microsoft's suggestion to include powershell core when build your own image.

Next is the Dockerfile about how nanoserver-18.09 to include powershell core, you could copy this to write your own dockerfile to add powershell core to your aspnet:3.1-nanoserver-1809:

# escape=`# Args used by from statements must be defined here:ARG InstallerVersion=nanoserverARG InstallerRepo=mcr.microsoft.com/powershellARG 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 awayFROM ${InstallerRepo}:$InstallerVersion  AS installer-env# Arguments for installing PowerShell, must be defined in the container they are usedARG PS_VERSION=7.0.0-rc.1ARG PS_PACKAGE_URL=https://github.com/PowerShell/PowerShell/releases/download/v$PS_VERSION/PowerShell-$PS_VERSION-win-x64.zip# disable telemetryENV POWERSHELL_TELEMETRY_OPTOUT="1"SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]ARG PS_PACKAGE_URL_BASE64RUN 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 NanoServerFROM ${NanoServerRepo}:1809ARG IMAGE_NAME=mcr.microsoft.com/powershell# Copy PowerShell Core from the installer containerENV 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://docs.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#L76USER ContainerAdministrator# This is basically the correct code except for the /MRUN setx PATH "%PATH%;%ProgramFiles%\PowerShell;" /MUSER ContainerUser### End workaround ###COPY --from=installer-env ["\\PowerShell\\", "$ProgramFiles\\PowerShell"]# intialize powershell module cacheRUN 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 telemetryENV POWERSHELL_TELEMETRY_OPTOUT="0"CMD ["pwsh.exe"]