Skip to content

Instantly share code, notes, and snippets.

@brettmillerb
Created April 5, 2019 21:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettmillerb/fa4c18be41acc69cc19f97ad9899d2c3 to your computer and use it in GitHub Desktop.
Save brettmillerb/fa4c18be41acc69cc19f97ad9899d2c3 to your computer and use it in GitHub Desktop.
Pwsh Dockerfile
# escape=`
ARG fromTag=latest
ARG WindowsServerCoreRepo=mcr.microsoft.com/windows/servercore
# 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 ${WindowsServerCoreRepo}:${fromTag} AS installer-env
ARG PS_VERSION=6.2.0
ARG PS_PACKAGE_URL=https://github.com/PowerShell/PowerShell/releases/download/v${PS_VERSION}/PowerShell-${PS_VERSION}-win-x64.zip
SHELL ["C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "-command"]
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; `
Invoke-WebRequest -Uri $url -outfile /powershell.zip -verbose; `
Expand-Archive powershell.zip -DestinationPath \PowerShell
# Install PowerShell into WindowsServerCore
FROM ${WindowsServerCoreRepo}:${fromTag}
# Copy PowerShell Core from the installer container
ENV ProgramFiles="C:\Program Files" `
# set a fixed location for the Module analysis cache
LOCALAPPDATA="C:\Users\ContainerAdministrator\AppData\Local" `
PSModuleAnalysisCachePath="$LOCALAPPDATA\Microsoft\Windows\PowerShell\docker\ModuleAnalysisCache" `
# Persist %PSCORE% ENV variable for user convenience
PSCORE="$ProgramFiles\PowerShell\pwsh.exe"
# Copy PowerShell Core from the installer container
COPY --from=installer-env ["\\PowerShell\\", "$ProgramFiles\\PowerShell\\latest"]
# Set the path
RUN setx /M PATH "%ProgramFiles%\PowerShell\latest;%PATH%"
# 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 ; `
}"
RUN pwsh `
-NoLogo `
-NoProfile `
-Command " `
Set-ExecutionPolicy Bypass -Scope Process -Force; `
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
RUN pwsh `
-NoLogo `
-NoProfile `
-Command " `
choco install git -y; `
refreshenv"
CMD ["pwsh.exe"]
ARG IMAGE_NAME=mcr.microsoft.com/powershell:windowsservercore
ARG VCS_REF="none"
ARG PS_VERSION=6.2.0
LABEL maintainer="PowerShell Team <powershellteam@hotmail.com>" `
readme.md="https://github.com/PowerShell/PowerShell/blob/master/docker/README.md" `
description="This Dockerfile will install the latest release of PowerShell." `
org.label-schema.usage="https://github.com/PowerShell/PowerShell/tree/master/docker#run-the-docker-image-you-built" `
org.label-schema.url="https://github.com/PowerShell/PowerShell/blob/master/docker/README.md" `
org.label-schema.vcs-url="https://github.com/PowerShell/PowerShell-Docker" `
org.label-schema.name="powershell" `
org.label-schema.vendor="PowerShell" `
org.label-schema.vcs-ref=${VCS_REF} `
org.label-schema.version=${PS_VERSION} `
org.label-schema.schema-version="1.0" `
org.label-schema.docker.cmd="docker run ${IMAGE_NAME} pwsh -c '$psversiontable'" `
org.label-schema.docker.cmd.devel="docker run ${IMAGE_NAME}" `
org.label-schema.docker.cmd.test="docker run ${IMAGE_NAME} pwsh -c Invoke-Pester" `
org.label-schema.docker.cmd.help="docker run ${IMAGE_NAME} pwsh -c Get-Help"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment