Forum Discussion

ooker's avatar
ooker
Copper Contributor
Sep 03, 2023

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\Docker Desktop.exe"

Start-Process $DockerDesktopPath
$id = (Get-Process 'Docker Desktop').id
Wait-Process -id $id

docker start $ContainerName
docker wait $ContainerName

$IsContainerRunning = docker container inspect -f '{{.State.Running}}' $ContainerName
$IsDockerDesktopRunning = Get-Process "Docker Desktop" -ErrorAction SilentlyContinue

while ($IsContainerRunning -eq $true) {
    Start-Sleep -Seconds 5
    $IsContainerRunning = docker container inspect -f '{{.State.Running}}' $ContainerName
}

if ($IsDockerDesktopRunning && $IsContainerRunning) {
    $IsDockerDesktopRunning.CloseMainWindow()
    $IsDockerDesktopRunning | Stop-Process -Force
}

wsl --shutdown

Currently, I’m stuck at this part:

Start-Process $DockerDesktopPath
$id = (Get-Process 'Docker Desktop').id
Wait-Process -id $id

If I don’t wait for the process to finish, then I can’t call docker start. However, if I do that, then it won’t finish even though Docker Desktop has already popped up and is doing nothing. I also want to have the log of the container to be displayed in the host’s terminal. Any suggestion?

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    ooker 

     

    I don't know anything about Docker, but I can try and help with your first question (though not the second question about logging).

     

    When you say that docker start fails, what does that look like? Is it just that nothing happens, or do you get a detailed error message?

     

    If you put a large delay after the Start-Process (i.e. Start-Sleep -Seconds 20) and before the docker start, does the the docker start work then?

     

    Do you know if the Docker desktop process spawns any child processes you can monitor for?

     

    Knowing some of this may help us help you with the first question.

     

    Cheers,

    Lain

    • ooker's avatar
      ooker
      Copper Contributor
      well, technically I can use sleep, but to learn more about scripting, is there a way to detect it when an event from a program happens? Say I open Word, is there a way to trigger a script when it finishes the startup?
      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        ooker 

         

        There's no single, generic way to know that.

         

        At best, there might be a way that's specific to the exact application/service you're working with, but for another application/service, it might require a completely different approach - or there may be no way at all. It's not like Windows prescribes a standard that client processes have to confirm to/integrate with.

         

        Take Docker, for example. If the "Docker Desktop" you're launching were to in turn launch (or stop) child processes of its own, then you could leverage that behaviour. But let's say it doesn't, then you may have no visibility into how it's going with its start-up routine at which point all you can do is wait an arbitrary interval (or variations of that such as polling, but this isn't always materially any better).

         

        Cheers,

        Lain

Resources