My laptop ran out of space this morning. I had noticed some slowness last week, but I attributed that to some weird experiment I was running on Kubernetes. This morning, however, I received some rather alarming error messages from Windows that it couldn't write log files and other temp things.
I immediately went to work cleaning up stuff in Windows. One of the things that I cleaned up was my Docker installtion. In this post I'll show you how I did it as a friendly reminder to clean up your garbage from time to time 😅.
Let's get started!
Step 1: Clean up docker objects
The first step to reclaim space is to remove any old cruft generated by Docker. Run the following command with Docker Desktop running:
docker system prune -a
This removes any old images, networks, volumes, etc. from your Docker installation. In my case I got a whopping 57GB back with this operation. Let's just say that it's been a while since I last checked.
Removing the cruft from Docker Desktop doesn't help much though without the next step. Docker Desktop uses a VHDX file to store its items on and you'll only remove files from that virtual hard drive.
After you've removed the files from the VHDX, you're going to need to shrink the VHDX file too.
Step 2: Clean up the WSL installation for Docker
Let's shrink the VHDX used by Docker Desktop. Make sure Docker Desktop is shutdown. You'll also want to shutdown WSL. You can do this using the following command:
wsl --shutdown
After everything is shut down, you can execute the following command in an administrative powershell instance:
optimize-vhd C:\users\<account-name>\appdata\local\docker\wsl\data\ext4.vhdx -mode full
Make sure to replace <account-name>
with the account name you're using in Windows.
Note that it will take a while to shrink the VHDX file.
Summary
After garbage collecting my docker files and shrinking the VHDX, I went from a 134GB file to a 36GB file for my Docker Desktop installation. Much better!
It goes without saying that making a backup of your files is a good idea before running things in an administrative prompt. Don't say I didn't warn you!
Enjoy your freed diskspace.