How To Reduce Size Of Docker Data Volume In Docker Desktop For Windows

Photo by CHUTTERSNAP / Unsplash

How To Reduce Size Of Docker Data Volume In Docker Desktop For Windows

Resize your Docker Data Volume with one command and reclaim a lot of space on your HDD

Paul Knulst  in  Docker Jul 1, 2022 2 min read

If you use Docker Desktop for Windows that uses WSL2 all your images and container files are stored in a separate virtual volume (vhdx).

This virtual hard disk has the ability to automatically grow when it needs more space until it reaches a certain limit. But if you reclaim space by using docker system prune --all or docker volume prune the vhdx does not shrink automatically. Windows offers two ways to manually shrink the virtual disk so that it releases unused space.

1. Use Optimize-VHD

The Optimize-VHD cmdlet can be used to optimize the allocation of space in one or more virtual hard disk files. It reclaims unused blocks as well as rearranges the blocks to be more efficiently packed, which will reduce the overall size of a virtual hard disk file. But you cannot resize fixed virtual hard disks.

To start the process switch to a PowerShell (as Administrator) and execute:

Optimize-VHD -Path "path to your ext4.vhdx" -Mode Full

If you don't move docker-desktop-data to a new location (read this part of my article to know how to move it) you can find it in this location:

%LOCALAPPDATA%\Docker\wsl\data\ext4.vhdx

2. Use Diskpart To Resize VHD

If you cannot use Optimize-VHD on your Windows (often this cannot be done in Windows Home) you can use another approach to resize your docker-desktop-data ext4.vhdx virtual disk.

You will use diskpart to compact the virtual disk.

To do this open a Terminal and shut down your Windows Subsystem for Linux

wsl --shutdown

Afterward, you can start diskpart and select your docker-desktop ext4.vhdx

diskpart
select dockhdd file="path to your ext4.vhdx"

As an identifier to work with I chose vdisk but you can name it as you want.

Now you can attach, compact, detach the virtual disk, and close diskpart.

attach dockhdd readonly
compact dockhdd

[ ... this could take some time ]

detach dockhdd
close

Closing Notes

I hope you find this article about resizing the Docker virtual hard disk helpful and are able to replicate it.

I would love to hear your ideas and thoughts about these methods. If you know another approach or have any questions, please jot them down below. I try to answer them if possible.


🙌 Support this content

If you like this content, please consider supporting me. You can share it on social media or buy me a coffee! Any support helps!

See the contribute page for all (free or paid) ways to say thank you!

Thanks! 🥰

By Paul Knulst

I'm a husband, dad, lifelong learner, tech lover, and Senior Engineer working as a Tech Lead. I write about projects and challenges in IT.