Last active
February 27, 2025 12:52
-
-
Save pbering/309c5a14162dcbe70e842295e0319022 to your computer and use it in GitHub Desktop.
Installs (and updates) Docker engine, compose and the credentials helper on Windows server and client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run with defaults: iex (iwr "https://gist.githubusercontent.com/pbering/309c5a14162dcbe70e842295e0319022/raw" -UseBasicParsing).Content | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string]$DockerVersion = "27.5.1" | |
, | |
[Parameter(Mandatory = $false)] | |
[string]$ComposeVersion = "2.33.1" | |
, | |
[Parameter(Mandatory = $false)] | |
[string]$WinCredVersion = "0.8.2" | |
) | |
$ErrorActionPreference = "Stop" | |
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) | |
{ | |
throw "This script needs to be run as admin." | |
} | |
$dockerUrl = "https://download.docker.com/win/static/stable/x86_64/docker-$DockerVersion.zip" | |
$composeUrl = "https://github.com/docker/compose/releases/download/v$ComposeVersion/docker-compose-Windows-x86_64.exe" | |
$winCredUrl = "https://github.com/docker/docker-credential-helpers/releases/download/v$WinCredVersion/docker-credential-wincred-v$WinCredVersion.windows-amd64.exe" | |
$installDir = "C:\Program Files\Docker" | |
$pluginsDir = "$installDir\cli-plugins" | |
# ensure the containers feature is installed | |
$isWindowsClientOS = (Get-CimInstance -ClassName Win32_OperatingSystem).ProductType -eq 1 | |
$rebootNeeded = $false | |
if ($isWindowsClientOS) | |
{ | |
if ((Get-WindowsOptionalFeature -Online -FeatureName "Containers").State -eq "Disabled") | |
{ | |
Enable-WindowsOptionalFeature -FeatureName "Containers" -All -Online -NoRestart | |
$rebootNeeded = $true | |
} | |
} | |
else | |
{ | |
if (!(Get-WindowsFeature Containers).Installed) | |
{ | |
Install-WindowsFeature Containers | |
$rebootNeeded = $true | |
} | |
} | |
if ($rebootNeeded) | |
{ | |
Write-Warning "Containers feature installed, please reboot and run again..." | |
return | |
} | |
Write-Host "### Installing Docker $DockerVersion, Compose $ComposeVersion, wincred $WinCredVersion..." | |
# validating versions | |
$versionsValid = $true | |
$dockerUrl, $composeUrl, $winCredUrl | ForEach-Object { | |
$notfound = curl.exe --head -s $_ | Select-String "HTTP/1.1 404" -Quiet | |
if ($notfound -eq $true) | |
{ | |
Write-Warning "Not found: $_" | |
$versionsValid = $false | |
} | |
} | |
if (!$versionsValid) | |
{ | |
throw "One or more urls was not found." | |
} | |
# ensure install directory | |
if (!(Test-Path $installDir)) | |
{ | |
New-Item -Path $installDir -ItemType Directory | Out-Null | |
} | |
# ensure plugins directory | |
if (!(Test-Path $pluginsDir)) | |
{ | |
New-Item -Path $pluginsDir -ItemType Directory | Out-Null | |
} | |
# ensure PATH | |
if (!$env:Path.Contains( "$installDir;") ) | |
{ | |
$env:Path = "{0};$installDir" -f $env:Path | |
[Environment]::SetEnvironmentVariable("PATH", $env:Path, [EnvironmentVariableTarget]::Machine) | |
} | |
# stop and remove existing docker service | |
if (Get-Service "docker" -ErrorAction "SilentlyContinue") | |
{ | |
Stop-Service "docker" | |
& "$installDir\dockerd.exe" --unregister-service | |
} | |
# install docker | |
Start-BitsTransfer $dockerUrl -Destination "$installDir\docker.zip" | |
Expand-Archive -Path "$installDir\docker.zip" -DestinationPath (Get-Item $installDir).Parent.FullName -Force | |
& "$installDir\dockerd.exe" --register-service | |
Start-Service "docker" | |
# install compose | |
Start-BitsTransfer $composeUrl -Destination "$pluginsDir\docker-compose.exe" | |
# install wincred | |
Start-BitsTransfer $winCredUrl -Destination "$installDir\docker-credential-wincred.exe" | |
# done | |
Write-Host "### Finished!" | |
docker info |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment