Previous tutorials didnt worked for me because my setup is a little bit odd.
I'm using windows 11 machine with WSL2 and docker desktop.
I also use k3d to create kubernetes cluster. Also my k3d cluster have local registry
I also used skaffold and other shinigens
%USERPROFILE%\AppData\Local\Docker\wsl\disk\ - there is your docker_data.vhdx
%USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ - there is your WSL Ubuntu
Check both files size, in my case docker_data.vhdx had 250GB
First try do "normal stuff" like going into docker, delete not used volumes, images, buildcache etc. You can delete this using Docker Desktop or like a hacker run this:
docker system prune -a
This will delete all unused images not just dangling ones.
After this You can check how many data you was able to restore. In normal cases this is enough.
- Stop Docker desktop (and wait for it to fully close)
- Stop wsl
wsl --shutdown - Check if wsl is closed
wsl --list --running(if not, shutdown again) I often forgot about opened vscode that is connected to WSL. Close it too
Now we can shrink drive!
Open powershell as administartor sudo powershell
optimize-vhd C:\Users\USER\AppData\Local\Docker\wsl\disk\docker_data.vhdx -Mode Full
optimize-vhd C:\Users\USER\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc\LocalState\ext4.vhdx -Mode Full
Perfect you shrinked your drives successfuly!
But in my case that didnt worked. Why? because when you use k3d you have another docker inside k3d. You only removed stuff from "outside" docker. We need to delete stuff that is inside!
Open Docker desktop and find registry.localhost (or similar container created by k3d).
Open terminal of that container and run:
registry garbage-collect /etc/docker/registry/config.yml -m
That will delete all images without tags. In my case it left only :latest images.
Perfect, that saved me 60GB
Do you remmember that I said that your k3d have inside another docker? It lives there inside k3d-{...}-server-0 .
Open Docker desktop and find that container, open terminal ad run:
crictl rmi --prune
That will remove all build cache, dangling images etc. For me it removed old images and buildcache.
That way I saved 120GB
After this shinigens above you can finaly try shrink drives.
Remmember to stop Docker Desktop, stop wsl etc.
sudo powershell
wsl --shutdown
wsl --list --running
optimize-vhd %USERPROFILE%\AppData\Local\Docker\wsl\disk\docker_data.vhdx -Mode Full
And finaly it went down from 250GB to 95GB where I have only stuff that I need. Also I dont need to recreate whole cluster :-)
- For windows I recommend awesome WinDirStat https://windirstat.net/
- For linux I recommend awesome
ncduhttps://en.wikipedia.org/wiki/Ncdu
This 2 tools saved me a lot time
For windows, WizTree is really nice in comparison to WinDirStat.