Forum Discussion
ooker
Sep 03, 2023Copper Contributor
How to write a script to start a Docker container and stop WSL safely when the container stops?
I’m writing a PowerShell script to start a container and stop WSL safely when the container stops. Here is my attempt: $ContainerName = 'tranky'
$DockerDesktopPath = "C:\Program Files\Docker\Docker\...
ooker
Sep 06, 2023Copper Contributor
I see. So if an app wants to implement this, what would be the way for it to do so? What teens should I look for?
LainRobertson
Sep 07, 2023Silver Contributor
I don't quite understand the question. What are teens? (Excluding rambunctious kids.)
Cheers,
Lain
- ookerSep 07, 2023Copper Contributor
oh it's autocorrect
. I was asking "what terms should I look for?"
- LainRobertsonSep 07, 2023Silver Contributor
I can't tell you what to look for with Docker, as I don't use it.
The best I can do is provide you with a hypothetical example for a scenario where the first program you start with Start-Process (in your case, this is Docker Desktop) then starts another child process of its own, indicating it's operational.
Because I don't use Docker, I have to use other programs in the example. Here's a table that translates what I'm using for illustrative purposes to what you would use.
Stage My example Your scenario Start-Process cmd.exe Docker Desktop Child process notepad.exe <something Docker launches> Example
# Launch the "main" process. $Process = Start-Process -FilePath "cmd.exe" -PassThru -ErrorAction:Stop; # Monitor for a specific child process being launched by the main process. $CheckAgain = $true; $Retries = 10; while ($CheckAgain -and ($Retries -gt 0)) { if (Get-CimInstance -ClassName "Win32_Process" -Filter "ParentProcessID=$($Process.Id) AND Name='notepad.exe'") { $CheckAgain = $false; } else { $Retries--; Start-Sleep -Seconds 2; } }
Here, we're checking for a maximum of 20 seconds ([10 retries] x [two second sleep] to see if the child process has started. If it starts earlier, then the loop will exit earlier.
But again, this is hypothetical and may not be useful for your Docker scenario at all. You'd have to do some investigating to see how Docker behaves before you can solve your challenge.
Cheers,
Lain
- ookerSep 08, 2023Copper Contributor
Actually I was broadening my topic and not talking about Docker specifically. Like, if I'm writing a program and want to register my program's events to the OS so that users can take it from there, what should I do? Only signalling via child process seems limited to me. I guess the only way to communicate with the OS is to pack all the information in the process title. I see that the Task Scheduler is something in the right way.