Cleaning disk space after docker

Using docker on your computer, you can notice how actively it uses space for containers and images. Sooner or later, the moment comes when it's time to clean the whole place.

If you are using linux, then you have no problems, just use the command:

docker system prune

But it doesn't work on windows, why? The problem is that docker stores all its data inside a virtual disk.vhdx which can grow independently, but not decrease in size. But it doesn't matter, because there is a way to manually compress it.

To begin with, let's decide on the windows edition, because the decision depends on it.

If you have Windows Home Edition

  1. Open the PowerShell window as an administrator
  2. Determine where the docker disk file is located By default, it is stored on the path "C:\Users\User\AppData\Local\Docker\wsl\data\ext4.vhdx "
  3. Shut down Windows Subsystem for Linux with the command
wsl --shutdown
  1. Run the DiskPart utility
diskpart
  1. Select the file with the docker disk
select vdisk file="C:\Users\User\AppData\Local\Docker\wsl\data\ext4.vhdx"
attach vdisk readonly
  1. Perform compression, it may take quite a long time, depending on the weight of the file
compact vdisk
  1. Shut down the DiskPart utility
detach vdisk
exit

If you don't have Windows Home Edition

  1. Open the PowerShell window as an administrator
  2. Connect the Hyper-V module in PowerShell
Import-Module Hyper-V
  1. Determine where the docker disk file is located By default, it is stored on the path "C:\Users\User\AppData\Local\Docker\wsl\data\ext4.vhdx "
  2. Shut down Windows Subsystem for Linux with the command
wsl --shutdown
  1. Go to the directory with the disk
cd "C:\Users\User\AppData\Local\Docker\wsl\data \"
  1. Perform compression, it may take quite a long time, depending on the weight of the file
optimize-vhd -Path .\ext4.vhdx -Mode full

Just like that, we freed up a couple (dozens) gigabytes on our disk.