Adding setup to use docker within wsl

This commit is contained in:
Zackarias Montell
2026-05-05 08:43:53 +02:00
parent 8441afb56f
commit e850b16df6
3 changed files with 56 additions and 0 deletions

51
Update-WSLDockerProxy.ps1 Normal file
View File

@@ -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
}