docker
1 TopicHow 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?2.2KViews0likes9Comments