Or: “WSL2 Docker is not connecting to my Host!!!! HELP!”
In another post i already talked about a setup to get both Windows Host / Windows Host Docker and WSL Docker talking to each other.
The setup broke at some point and i figured out a pretty automated solution for this… for now, let’s hope it works after the next WSL or Docker update.
My solution was following Setup:
1- Start Docker in WSL automatically on BOOT
2- Wait a minute after Logon and start Docker Desktop (this post)
Prerequisites:
– Docker Desktop WSL Integration is turned off.
– Docker on WSL has been detached from Docker Desktop for the Windows Host
– Docker Desktop Autostart is disabled
Now, assuming you followed part 1.
Let’s continue and create a file and paste this powershell script provided by StackOverflow User StingyJack (Thanks m8!).
In my case, the file is running in “C:/Sources/Scripts_Autostart” and is called “docker-restart.ps1”.
Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Restarting docker"
foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Running"}))
{
$svc | Stop-Service -ErrorAction Continue -Confirm:$false -Force
$svc.WaitForStatus('Stopped','00:00:20')
}
Get-Process | Where-Object {$_.Name -ilike "*docker*"} | Stop-Process -ErrorAction Continue -Confirm:$false -Force
foreach($svc in (Get-Service | Where-Object {$_.name -ilike "*docker*" -and $_.Status -ieq "Stopped"} ))
{
$svc | Start-Service
$svc.WaitForStatus('Running','00:00:20')
}
Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Starting Docker Desktop"
& "C:\Program Files\Docker\Docker\Docker Desktop.exe"
$startTimeout = [DateTime]::Now.AddSeconds(90)
$timeoutHit = $true
while ((Get-Date) -le $startTimeout)
{
Start-Sleep -Seconds 10
$ErrorActionPreference = 'Continue'
try
{
$info = (docker info)
Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info executed. Is Error?: $($info -ilike "*error*"). Result was: $info"
if ($info -ilike "*error*")
{
Write-Verbose "$((Get-Date).ToString("HH:mm:ss")) - `tDocker info had an error. throwing..."
throw "Error running info command $info"
}
$timeoutHit = $false
break
}
catch
{
if (($_ -ilike "*error during connect*") -or ($_ -ilike "*errors pretty printing info*") -or ($_ -ilike "*Error running info command*"))
{
Write-Output "$((Get-Date).ToString("HH:mm:ss")) -`t Docker Desktop startup not yet completed, waiting and checking again"
}
else
{
Write-Output "Unexpected Error: `n $_"
return
}
}
$ErrorActionPreference = 'Stop'
}
if ($timeoutHit -eq $true)
{
throw "Timeout hit waiting for docker to startup"
}
Write-Output "$((Get-Date).ToString("HH:mm:ss")) - Docker restarted"
Next up: Open Task Scheduler and create a new Basic Task
Name it like you want, i chose “Start Docker Desktop after WSL Docker”.
Next, then select “When i log on”
Next: set “Program/Script” to Powershell
Then set argument to
-windowstyle hidden -ExecutionPolicy Bypass -file C:\Sources\Scripts_Autorun\docker-restart.ps1
Of course you will choose the folder where you put the script.
don’t forget to add quotations if your path contains spaces!
Click next and after finishing the setup open the Task back up again. (Edit properties)
In the General Tab set “Run only when user is logged on” and “Run on highest privilegues”
Open the Trigger in the “Triggers” tab and set a Delay for 1 minute.
That’s it!
Have, of course you will have to chill about a minute or two after rebooting your pc.
If you’re faster than your scripts, this won’t work.
You might also want to adjust the delay time for the script for part 1. and 2.
Recent Comments