a pleasant descent into madness


WSL2 Docker not talking to Windows Host

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)

– 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

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 

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'
        $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

        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"
            Write-Output "Unexpected Error: `n $_"
    $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.

Start WSL 2 Service on Host startup

Or: “I don’t want to manually startup everything in WSL after every boot”

For me, that’s docker.
My docker is running independantly of docker desktop on the host so i want it to startup when the PC is booted.

Following easy steps to set it up in Task Scheduler:

  • Open up Task Scheduler
  • Create new Basic Task
  • Name it like you will, mine was “Autostart of WSL Docker service on boot”
  • Next, then choose “When the Computer Starts”
  • Next, then “Start a program”
  • In “Program/script” find WSL.exe, it should be located like so:
  • In “Add arguments (optional)” enter following:
    -d Ubuntu-20.04 -u root service docker start
  • Finish the setup via “Next” then “Finish”.
  • Run it to test it.

Instead of “Ubuntu-20.04” you might want to find your correct distro name by entering
“wsl –list” into a commandline on windows, you’ll need to use the same distro name displayed for the distro of your choosing.

if this doesn’t immediately work for you, you might want to open up the task in task-scheduler, navigate to “triggers”, open the trigger and set “Delay Task for” to 30 seconds or any amount that makes sense for your system start-up time.

That’s it.
Many thanks to StackOverflow user octagon_octopus

© 2022 Yavuz-Support Blog

Theme by Anders NorenUp ↑