From e850b16df6576db98bac1ffd68fe72a5566aa027 Mon Sep 17 00:00:00 2001 From: Zackarias Montell Date: Tue, 5 May 2026 08:43:53 +0200 Subject: [PATCH] Adding setup to use docker within wsl --- .profile.ps1 | 1 + Register-WSLDockerProxyStartupTask.ps1 | 4 ++ Update-WSLDockerProxy.ps1 | 51 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 Register-WSLDockerProxyStartupTask.ps1 create mode 100644 Update-WSLDockerProxy.ps1 diff --git a/.profile.ps1 b/.profile.ps1 index ba8b2ea..92454b4 100644 --- a/.profile.ps1 +++ b/.profile.ps1 @@ -207,6 +207,7 @@ Set-Alias -Name notes -Value Open-Notes # Set up docker environment #$Env:DOCKER_HOST="ssh://wholteza@localhost:22/" +[System.Environment]::SetEnvironmentVariable("DOCKER_HOST", "tcp://localhost:2375", "User") # Set all files in .kube as kubeconfig paths. # This allows for dynamic kubeconfig use. diff --git a/Register-WSLDockerProxyStartupTask.ps1 b/Register-WSLDockerProxyStartupTask.ps1 new file mode 100644 index 0000000..408096f --- /dev/null +++ b/Register-WSLDockerProxyStartupTask.ps1 @@ -0,0 +1,4 @@ +$script = "C:\Users\$env:USERNAME\Update-WSLDockerProxy.ps1" + +Start-Process powershell.exe -Verb RunAs ` + -ArgumentList "-NonInteractive -Command `"Register-ScheduledTask -TaskName 'WSL Docker Proxy' -Action (New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-NonInteractive -WindowStyle Hidden -File \`"$script\`"') -Trigger (New-ScheduledTaskTrigger -AtLogOn) -RunLevel Highest -Force`"" diff --git a/Update-WSLDockerProxy.ps1 b/Update-WSLDockerProxy.ps1 new file mode 100644 index 0000000..56515d0 --- /dev/null +++ b/Update-WSLDockerProxy.ps1 @@ -0,0 +1,51 @@ +# Update-WSLDockerProxy.ps1 +# Updates the portproxy rule for WSL Docker, self-elevating if needed. +# Usage: . .\Update-WSLDockerProxy.ps1 (dot-source to import the function) +# Update-WSLDockerProxy (then call it) +# Or just run the script directly and it will execute automatically. + +function Update-WSLDockerProxy { + [CmdletBinding()] + param( + [int]$Port = 2375 + ) + + # Self-elevate if not running as admin + if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { + Write-Host "Not running as admin, re-launching elevated..." -ForegroundColor Yellow + $scriptPath = $MyInvocation.MyCommand.ScriptBlock.File + if (-not $scriptPath) { + Write-Error "Cannot self-elevate when dot-sourced without a file path. Run the script directly instead." + return + } + Start-Process powershell.exe -Verb RunAs -ArgumentList "-NonInteractive -WindowStyle Hidden -File `"$scriptPath`" -Port $Port" + return + } + + # Get current WSL IP + $WSL_IP = (wsl hostname -I 2>$null).Split(" ")[0].Trim() + + if (-not $WSL_IP) { + Write-Error "Could not get WSL IP. Is WSL running?" + return + } + + Write-Host "WSL IP: $WSL_IP" -ForegroundColor Cyan + + # Remove existing rule if present + $existing = netsh interface portproxy show v4tov4 | Select-String ":$Port" + if ($existing) { + Write-Host "Removing existing portproxy rule on port $Port..." -ForegroundColor Gray + netsh interface portproxy delete v4tov4 listenport=$Port listenaddress=127.0.0.1 | Out-Null + } + + # Add new rule + netsh interface portproxy add v4tov4 listenport=$Port listenaddress=127.0.0.1 connectport=$Port connectaddress=$WSL_IP | Out-Null + + Write-Host "Portproxy updated: 127.0.0.1:$Port -> ${WSL_IP}:$Port" -ForegroundColor Green +} + +# Run automatically if script is executed directly (not dot-sourced) +if ($MyInvocation.InvocationName -ne '.') { + Update-WSLDockerProxy +}