Skip to content

Instantly share code, notes, and snippets.

@pbering
Last active June 21, 2024 12:56
Show Gist options
  • Save pbering/309c5a14162dcbe70e842295e0319022 to your computer and use it in GitHub Desktop.
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
# run with defaults: iex (iwr "https://gist.githubusercontent.com/pbering/309c5a14162dcbe70e842295e0319022/raw" -UseBasicParsing).Content
param (
[Parameter(Mandatory = $false)]
[string]$DockerVersion = "26.1.4"
,
[Parameter(Mandatory = $false)]
[string]$ComposeVersion = "2.28.0"
,
[Parameter(Mandatory = $false)]
[string]$WinCredVersion = "0.8.0"
)
$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