Skip to content

Instantly share code, notes, and snippets.

@adujardin
Created January 6, 2026 17:20
Show Gist options
  • Select an option

  • Save adujardin/60d65737d85452e9283ce5084eac876b to your computer and use it in GitHub Desktop.

Select an option

Save adujardin/60d65737d85452e9283ce5084eac876b to your computer and use it in GitHub Desktop.
Windows gitlab docker runner maintenance script to avoid "Insufficient system resources exist to complete the requested service."
$TaskName = "GitLabRunnerMaintenance"
$ScriptPath = "C:\GitLab-Runner\maintenance.ps1"
# 1. Define the Action (Run PowerShell)
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$ScriptPath`""
# 2. Define the Trigger (e.g., Daily at 4:00 AM)
$Trigger = New-ScheduledTaskTrigger -Daily -At 4:00AM
# 3. Define the User (Run as SYSTEM - essential for restarting services)
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
# 4. Define Settings (Don't stop if on battery, wake to run, etc.)
$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
# 5. Register the Task
Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings -TaskName $TaskName -Description "Daily cleanup of Docker and HNS to prevent resource exhaustion"
Write-Host "Task '$TaskName' created successfully."
# C:\GitLab-Runner\maintenance.ps1
# 1. Prune Docker (Forcefully remove stopped containers, unused networks, and dangling images)
# We use --volumes to ensure we don't leak anonymous volumes, a common Windows Docker issue.
docker system prune -a -f --volumes
# 2. Restart Host Networking Service (Fixes "insufficient system resources" / port leaks)
Restart-Service hns -Force
# 3. Restart Host Compute Service (Fixes stuck container handles)
Restart-Service vmcompute -Force
# Optional: Log that this ran
Add-Content -Path "C:\GitLab-Runner\maintenance.log" -Value "$(Get-Date): Maintenance ran successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment