Skip to content

Instantly share code, notes, and snippets.

@hakito
Created May 24, 2019 08:53
Show Gist options
  • Save hakito/52f40adae1aaf1a022e32934816e19a9 to your computer and use it in GitHub Desktop.
Save hakito/52f40adae1aaf1a022e32934816e19a9 to your computer and use it in GitHub Desktop.
Docker for Windows watchdog PowerShell script (NOT working)
#Requires -RunAsAdministrator
$containerHost = "localhost"
$containerPort = 480
$dockerVMHost = "10.0.75.2"
$restartCount = 0
while($true)
{
if (!(test-netconnection $containerHost -Port $containerPort).TcpTestSucceeded)
{
echo "Container connection lost."
docker-compose down
$processes = Get-Process "*docker for windows*"
if ($processes.Count -gt 0)
{
echo "Killing docker"
$processes[0].Kill()
$processes[0].WaitForExit()
}
echo "Stopping docker VM"
Stop-VM -Name MobyLinuxVM
echo "Starting docker VM"
Start-VM -Name MobyLinuxVM
echo "Starting Docker as $dockerUser"
runas /trustlevel:0x20000 "C:\Program Files\Docker\Docker\Docker for Windows.exe"
echo "Testing connection to docker VM"
while(!(test-netconnection $dockerVMHost).PingSucceeded)
{
Start-Sleep -Seconds 5
echo "Still waiting for docker VM to come up"
}
while(!(docker ps))
{
echo "Waiting for docker to respond"
Start-Sleep -Seconds 5
}
echo "Starting containers"
runas /trustlevel:0x20000 "docker-compose up -d"
echo "Testing container connection"
while(!(test-netconnection $containerHost -Port $containerPort).TcpTestSucceeded)
{
Start-Sleep -Seconds 5
echo "Still waiting for docker container to come up"
}
# TODO wait for docker to respond (probably using docker run)
$restartCount++
}
else
{
echo "Container $containerHost still running. $restartCount restarts"
}
Start-Sleep -Seconds 10
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment