-
Notifications
You must be signed in to change notification settings - Fork 8
/
Reset-DockerWslIntegration.ps1
94 lines (76 loc) · 2.16 KB
/
Reset-DockerWslIntegration.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
<#
.SYNOPSIS
This module provides functions to reset Docker WSL integration.
.DESCRIPTION
The module includes functions to stop Docker Desktop, stop WSL, and unregister Docker WSL data.
.NOTES
Version: 1.1
Author: Daethyra
#>
function Stop-DockerDesktop {
<#
.SYNOPSIS
Stops the Docker Desktop process.
.DESCRIPTION
Attempts to stop the Docker Desktop process. Writes an error if the process cannot be stopped.
#>
try {
Get-Process -Name "*Docker Desktop*" | Stop-Process -ErrorAction Stop
Write-Verbose "All Docker Desktop processes have stopped successfully."
}
catch {
Write-Error "Error stopping Docker Desktop processes: $_"
}
}
function Stop-Wsl {
<#
.SYNOPSIS
Shuts down the Windows Subsystem for Linux (WSL).
.DESCRIPTION
Tries to shut down WSL up to a maximum number of retries.
#>
param (
[int]$maxRetries = 1
)
for ($i = 0; $i -lt $maxRetries; $i++) {
wsl --shutdown
if ($?) {
Write-Verbose "WSL shut down successfully on attempt $($i + 1)."
return
}
else {
Write-Warning "WSL shutdown attempt $($i + 1) failed. Retrying..."
Start-Sleep -Seconds 2
}
}
Write-Error "Failed to shut down WSL after $maxRetries attempts."
}
function Unregister-DockerWsl {
<#
.SYNOPSIS
Unregisters the docker-desktop-data from WSL.
.DESCRIPTION
Attempts to unregister the docker-desktop-data. Writes an error if the operation fails.
#>
try {
wsl --unregister docker-desktop-data -ErrorAction Stop
Write-Verbose "Unregistered docker-desktop-data successfully."
}
catch {
Write-Error "Error unregistering docker-desktop-data: $_"
}
}
# Main script
try {
Write-Verbose "Starting Docker WSL integration reset process."
Write-Verbose "Stopping Docker Desktop"
Stop-DockerDesktop
Write-Verbose "Shutting down WSL"
Shutdown-Wsl
Write-Verbose "Unregistering docker-desktop-data"
Unregister-DockerWsl
Write-Host "Docker WSL integration successfully reset"
}
catch {
Write-Error "Reset failed: $_"
}