Proper docker cleanup on command line only

Hi,

we need to clean up docker images on our build infrastructure from time to time. I created a powershell script for this which is calling the command docker system prune --force This is deleting images and containers and tells me that there is freed up disk space. But as far as I read is that the virtual disk, used by the wsl has to be shrinked manually. I also integrated this into my script which is shrinking the virtual disk, but restarting docker desktop always ends up in throwing a error that wsl was terminated abruptly (which is true, as I had to shut it down for the shrink process). My question is, what is the clean way to remove all docker images and containers and release the disk space on windows using only command line commands.

Docker Dekstop: v4.35.1
OS: Windows 10 Enterprise 22H2

The VHDX file should be at

C:\Users\YOURUSER\AppData\Local\Docker\wsl\main\docker_data.vhdx

At least this is where I can see it in Docker Desktop v4.35.1. You can use Powershell tools like Optimize-VHD. You can find examples in Microsoft’s documentation

Or in old issues like this:

But the path was different.

thats exactly what I did. I have to shut down wsl for this and therefore the docker desktop application. When shutting down WSL, the docker desktop app complains (even though I killed all docker desktop processes) with a error message that wsl was terminated and requires manual interaction with the error dialog to continue. So following this process is not working in an automated environment.

So

  • you stopped Docker Desktop entirely, not just containers.
  • Then you shut down WSL as well (I didn’t know it was necessary. I would have stopped the related distributions only)
  • you used Optimize-VHD to decrease the size of the disk
  • Then you started Docker Desktop which warned you about that WSL was terminated
  • Docker Desktop could not start

Is this correct?

I don’t know any other way. When you use WSL2 as a backend, changing the disks currently require WSL-related operations on Windows. I read in the release notes that it was made easier on macOS, but I never had to try yet.