I have a docker application that we have deployed on a clients Windows 2022 server. As part of the clients security protocol the server does occasional restarts. I'd like the app to be redeployed automatically every time the server restarts.
We are using WSL 2 with docker installed directly on the WSL2 instance (not docker desktop).
Our approach is to have restart: always for all of our containers in our compose file. Then we have this powershell script that is run via a TaskScheduler task every time the server is restarted.
$distro = "Ubuntu-20.04"
$ports = @(80,443)
$log = "C:\scripts\wsl-startup.log"
function Log($m){ $ts=(Get-Date).ToString("s"); Add-Content -Path $log -Value "[$ts] $m" }
try {
Log "=== Boot script start ==="
Start-Sleep 45
Try { Set-Service iphlpsvc -StartupType Automatic } Catch {}
Start-Service iphlpsvc -ErrorAction SilentlyContinue
# Start the distro headlessly (keepalive + docker should keep it running)
Log "Starting WSL distro"
wsl -d $distro -- true | Out-Null
# Get WSL IPv4
$wslIp = $null
for ($i=0; $i -lt 12 -and -not $wslIp; $i++) {
$ipRaw = (wsl -d $distro hostname -I) 2>$null
Log ("hostname -I: '{0}'" -f $ipRaw)
$wslIp = ([regex]::Match($ipRaw,'\d+\.\d+\.\d+\.\d+')).Value
if (-not $wslIp) { Start-Sleep 5 }
}
if (-not $wslIp) { Log "ERROR: Could not determine WSL IPv4"; exit 1 }
Log ("WSL IPv4 = {0}" -f $wslIp)
foreach ($p in $ports) {
Log ("Setting portproxy :{0} -> {1}:{0}" -f $p, $wslIp)
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=$p 2>$null | Out-Null
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=$p connectaddress=$wslIp connectport=$p | Out-Null
}
Log "=== Boot script end ==="
exit 0
}
catch { Log ("EXCEPTION: {0}" -f $_.Exception.Message); exit 1 }
The script seems to be running successfully but the issue is the WSL instance does not persist hence I still need to log in and open a WSL instance on the server for the application to be visible.
Is there any better way of achieving what we are trying to do?
Happy to share more information if needed