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?
I think it requires more PowerShell and Windows skills than I have, but what is the purpose of this script? Why would you stop WSL instead of Docker Desktop?
Are you aware of the fact that wsl --shutdown “immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine”? How is that safe?
Are you aware of the fact that wsl --shutdown “immediately terminates all running distributions and the WSL 2 lightweight utility virtual machine”? How is that safe?
Oh I didn’t know that, thanks for the info. I thought that if Docker Desktop had been stopped, then I can stop WSL. I thought that stopping DD is like sending shutdown command to the host OS, and turning off WSL is like turning off the electricity. Is this analogy correct?
Ideally there should be a way to set a trigger to the host when a container stops, but I can’t find it out.
Well, apparently I’m blibnd. Sorry, I didn’t notice.
Docker Desktop has its own distributions When you stop Docker Desktop you stop those distributions and maybe some Windows services. I was never interested enough to check wsl --shutdown shuts down the virtual machine in which these and other distributions are, but actually I’m not surehow this command terminates the VM. For me “immediately terminates” means forcing to stop instead of gracefull shutdown. On the other hand, unless you enabled systemd in the distributions (not in DD distros) there is probably nothing running to stop so it could be safe to shut down WSL itself.
There were reports about WSL2 using too much resources, but doesn’t WSL give the resources back when you stop Docker Desktop?
Now back to your issue. I’m not a PowerShell guy but according to the documentation Wait-Process waits for the process to stop, not to start
Start-Process -Wait waits for the process to close like Wait-Process as well.
Technically I can use sleep, but to learn more about docker, is there a way to create a trigger when docker desktop completes the starting up, and when a container stops running?
I don’t know about any trigger, but Docker Desktop was created for development, not your for usecase and Docker Desktop users normaly don’t need a trigger like this. If you just want to run a container once a day and you don’t need the graphical interface, you could try other ways (like installing docker-ce in a WSL distro or running a VM manually or using Multipass) but since you already solved almost everything, you probably want to stick with Docker Desktop and the Powershell script.
I can only give you an idea which you can solve in Powershell, but I can’t tell you exactly how. You could remove Wait-Process and write a loop that runs docker version until it gets an actual server version. You can set a number how many times the script should try to get the version and how long it should wait between to tries.
This is also how processes can wait in a container for another service like a database to start.
When an event happens in Docker that means it happens in the Docker API but for that you first need to connect to it, which you can’t if the daemon is not running. If you want to be notified about a process started on the host operating system, that must be supported on by the host operating system. On Linux (without Docker Desktop), I would either try what I recommended for you or check the result of systemctl status docker.
On Windows, you could check the status of the Windows service but I’m not sure if that will return “running” only when the Docker daemon in the virtual machine is ready to accept connection or just shows when the Windows service started.
Pardon for my ignorance but which part did you recommend me earlier? I only see you discussed about Powershell, not Linux anywhere. Or are you referring to the bit about running docker-ce?
As for the systemctl status docker bit, it still seems to require you to check regularly whether the container stops or not. I’m thinking about having docker proactively notifies/sends signal to the host, like how webhook works. It seems to me that it’s not possible, is it correct?
I see. Well, can you confirm that beside systemctl status docker, there is a way to communicate Docker’s events to the OS or not? It seems strange to me that while docker can communicate with the containers, it cannot do that to the host. Since the status seems limited in the amount of info to me.
I can only repeat myself regarding events. If you can access to the Docker API you can access container events, but how the operating system handles events that’s a different story. I have no idea about Windows. On Linux, you could probably do something with Systemd, but I’m not sure about that either. When you run a process on Linux, that process can wait for its children, but I have never tried and never needed to subscribe to other events of independent processes. And I certainly don’t know any way to wait for a process that isn’t started yet unless there is a service that knows about those processes…
Sorry, I’m not sure what you mean. The docker client can communicate with the docker daemon through the docker API. The Docker daemon in your case is inside the virtual machine. The host for the docker daemon is the virtual machine, but you are trying to catch events on the physical host. Of course you can send a request from the container to the physical host (host.docker.internal) after finishing the task if an API server is listening on the host which would be responsible for stopping WSL.
systemctl status docker just shows the status of the process and some of the logs but I would use the status info only to determine whether the daemon is running or not. In your case it is not relevant on Windows and Docker Desktop.