-
Notifications
You must be signed in to change notification settings - Fork 55
/
CleanupAfterDocker.ps1
94 lines (75 loc) · 3.51 KB
/
CleanupAfterDocker.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
94
$ErrorActionPreference = "Stop"
$WarningActionPreference = "Stop"
# Specify which images to download
$ImagesToDownload = @()
$bcContainerHelperFolder = "C:\ProgramData\BcContainerHelper"
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (!($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) {
throw "This script must run with administrator privileges"
}
Write-Host "Checking Docker Service Settings..."
$dockerService = (Get-Service docker -ErrorAction Ignore)
if (!($dockerService)) {
throw "Docker Service not found / Docker is not installed"
}
if ($dockerService.Status -ne "Running") {
throw "Docker Service is $($dockerService.Status) (Needs to be running)"
}
$dockerInfo = (docker info)
$dockerOsMode = ($dockerInfo | Where-Object { $_.Trim().StartsWith('OSType: ') }).Trim().SubString(8)
if ($dockerOsMode -ne "Windows") {
throw "Docker is not running Windows Containers"
}
$dockerRootDir = ($dockerInfo | Where-Object { $_.Trim().StartsWith('Docker Root Dir: ') }).Trim().SubString(17)
if (!(Test-Path $dockerRootDir -PathType Container)) {
throw "Folder $dockerRootDir does not exist"
}
Write-Host -Foregroundcolor Red "This function will remove all containers, remove all images and clear the folder $dockerRootDir"
Write-Host -Foregroundcolor Red "The function will also clear the contents of $bcContainerHelperFolder."
Write-Host -Foregroundcolor Red "Are you absolutely sure you want to do this? (This cannot be undone)"
Write-Host -ForegroundColor Red "Type Yes to continue:" -NoNewline
if ((Read-Host) -ne "Yes") {
throw "Mission aborted"
}
Write-Host "Running Docker System Prune"
docker system prune -f
Write-Host "Removing all containers (forced)"
docker ps -a -q | % { docker rm $_ -f 2> NULL }
Write-Host "Stopping Docker Service"
stop-service docker
Write-Host "Downloading Docker-Ci-Zap"
$dockerCiZapExe = Join-Path $Env:TEMP "docker-ci-zap.exe"
Remove-Item $dockerCiZapExe -Force -ErrorAction Ignore
(New-Object System.Net.WebClient).DownloadFile("https://github.com/moby/docker-ci-zap/raw/master/docker-ci-zap.exe", $dockerCiZapExe)
Unblock-File -Path $dockerCiZapExe
Write-Host "Running Docker-Ci-Zap on $dockerRootDir"
Write-Host -ForegroundColor Yellow "Note: If this fails, please restart your computer and run this script again"
& $dockerCiZapExe -folder $dockerRootDir
Write-Host "Removing Docker-Ci-Zap"
Remove-Item $dockerCiZapExe
Write-Host "Starting Docker Service"
Start-Service docker
if (Test-Path $bcContainerHelperFolder -PathType Container) {
Write-Host "Cleaning up $bcContainerHelperFolder"
Get-ChildItem $bcContainerHelperFolder -Force | ForEach-Object {
Remove-Item $_.FullName -Recurse -force
}
}
if ($ImagesToDownload) {
Write-Host -ForegroundColor Green "Done cleaning up, pulling images for $os"
$os = "ltsc2016"
if ((Get-CimInstance win32_operatingsystem).BuildNumber -ge 17763) { $os = "ltsc2019" }
# Download images needed
$imagesToDownload | ForEach-Object {
if ($_.EndsWith('-ltsc2016') -or $_.EndsWith('-1709') -or $_.EndsWith('-1803') -or $_.EndsWith('-ltsc2019') -or
$_.EndsWith(':ltsc2016') -or $_.EndsWith(':1709') -or $_.EndsWith(':1803') -or $_.EndsWith(':ltsc2019')) {
$imageName = $_
} elseif ($_.Contains(':')) {
$imageName = "$($_)-$os"
} else {
$imageName = "$($_):$os"
}
Write-Host "Pulling $imageName"
docker pull $imageName
}
}