-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathInstallOrUpdateDockerEngine.ps1
93 lines (79 loc) · 3.69 KB
/
InstallOrUpdateDockerEngine.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Param(
[switch] $force,
[string] $envScope = "User",
[string] $dataRoot = 'C:\ProgramData\Docker'
)
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw "This script needs to run as admin"
}
if ((Test-Path (Join-Path $env:ProgramFiles "Docker Desktop")) -or (Test-Path (Join-Path $env:ProgramFiles "DockerDesktop"))) {
throw "Docker Desktop is installed on this Computer, cannot run this script"
}
# Install Windows feature containers
$restartNeeded = $false
if (!(Get-WindowsOptionalFeature -FeatureName containers -Online).State -eq 'Enabled') {
$restartNeeded = (Enable-WindowsOptionalFeature -FeatureName containers -Online -NoRestart).RestartNeeded
if ($restartNeeded) {
Write-Host "A restart is needed before you can start the docker service after installation"
}
}
# Get Latest Stable version and URL
$latestZipFile = (Invoke-WebRequest -UseBasicParsing -uri "https://download.docker.com/win/static/stable/x86_64/").Content.replace("`r",'').split("`n") |
Where-Object { $_ -like "<a href=""docker-*"">docker-*" } |
ForEach-Object { $zipName = $_.Split('"')[1]; [Version]($zipName.SubString(7,$zipName.Length-11).Split('-')[0]) } |
Sort-Object | Select-Object -Last 1 | ForEach-Object { "docker-$_.zip" }
if (-not $latestZipFile) {
throw "Unable to locate latest stable docker download"
}
$latestZipFileUrl = "https://download.docker.com/win/static/stable/x86_64/$latestZipFile"
$latestVersion = [Version]($latestZipFile.SubString(7,$latestZipFile.Length-11))
Write-Host "Latest stable available Docker Engine version is $latestVersion"
# Check existing docker version
$dockerService = get-service docker -ErrorAction SilentlyContinue
if ($dockerService) {
if ($dockerService.Status -eq "Running") {
$dockerVersion = [Version](docker version -f "{{.Server.Version}}")
Write-Host "Current installed Docker Engine version $dockerVersion"
if ($latestVersion -le $dockerVersion) {
Write-Host "No new Docker Engine available"
Return
}
Write-Host "New Docker Engine available"
}
else {
Write-Host "Docker Service not running"
}
}
else {
Write-Host "Docker Engine not found"
}
if (!$force) {
Read-Host "Press Enter to Install new Docker Engine version (or Ctrl+C to break) ?"
}
if ($dockerService) {
Stop-Service docker
}
# Download new version
$tempFile = "$([System.IO.Path]::GetTempFileName()).zip"
Invoke-WebRequest -UseBasicParsing -Uri $latestZipFileUrl -OutFile $tempFile
Expand-Archive $tempFile -DestinationPath $env:ProgramFiles -Force
Remove-Item $tempFile -Force
$path = [System.Environment]::GetEnvironmentVariable("Path", $envScope)
if (";$path;" -notlike "*;$($env:ProgramFiles)\docker;*") {
[Environment]::SetEnvironmentVariable("Path", "$path;$env:ProgramFiles\docker", $envScope)
}
# Register service if necessary
if (-not $dockerService) {
$dockerdExe = 'C:\Program Files\docker\dockerd.exe'
& $dockerdExe --register-service --data-root $dataRoot
}
New-Item $dataRoot -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
Remove-Item (Join-Path $dataRoot 'panic.log') -Force -ErrorAction SilentlyContinue | Out-Null
New-Item (Join-Path $dataRoot 'panic.log') -ItemType File -ErrorAction SilentlyContinue | Out-Null
try {
Start-Service docker
}
catch {
Write-Host -ForegroundColor Red "Could not start docker service, you might need to reboot your computer."
}