The problem of WSL2 disk space not being released after Windows docker deletes the container

The problem of WSL2 disk space not being released after Windows docker deletes the container

1. Cause and reason

After pulling the image and using it for a period of time, many students found that the C drive was almost full. After deleting the previously used image and container, they found that the size of the virtual disk in the WSL mount directory did not change, which was very strange.

In fact, unlike WSL1, WSL2 is essentially a virtual machine , so Windows will automatically create a virtual disk file with a vhdx suffix as storage. The feature of this vhdx suffix virtual disk file is that it can be automatically expanded, but generally it will not automatically shrink . Once there are many files to "enlarge" it, it will not automatically "shrink" even if these files are deleted. Therefore, after deleting files, we need to manually compress them to free up disk space.

2. Solve

2.1 Locate the virtual disk file to be compressed

If you haven’t changed the location of the mounted disk, then it’s there C:\Users\<你当前用户名>\AppData\Local\Docker\wsl\data\ext4.vhdx, write down the path, it will be used later.

2.2 Close Docker Desktop

Right-click the Docker Desktop icon in the lower right corner of the taskbar to close the Docker desktop, choose to exit the Docker desktop, wait for a while after the Docker icon disappears, it proves that Docker is completely closed, and then open the command prompt:

wsl --list -v

We'll be able to see, make sure both states are stopped.

insert image description here

If this step is not closed, there is no problem. After all operations are completed, restart Docker Desktop.

2.3 Compress virtual disk files

Execute in PowerShell:

# 关闭 WSL2 中的 linux distributions
wsl --shutdown
# 运行管理计算机的驱动器的 DiskPart 命令
diskpart

A new command window called DiskPart will be opened, as shown below:

insert image description here
Execute in the newly opened DiskPart command window:

# 选择虚拟磁盘文件
select vdisk file="就是步骤2.1虚拟磁盘文件的路径"
# 压缩文件
compact vdisk
# 压缩完毕后卸载磁盘
detach vdisk

After the above operations are completed, the disk space vacated by WSL2 after deleting the file will be released. You can go to the path of the virtual disk file to see that the size of the ext4.vhdx file has been reduced. Finally, open Docker Desktop and you can see that the original image is still there, and the problem is successfully solved.

3. Partial move

Some students try to delete ext4.vhdx to solve the problem directly from the root cause, in fact, this is also possible

  • Switch to the ext4.vhdx directory immediately after booting, and delete ext4.vhdx, because if you want to delete after starting docker, it will tell you that the ext4.vhdx operation cannot be completed because the file has been opened in System.
  • After the last step, we will find that the Docker Desktop has been starting and cannot be opened. Uninstall Docker Desktop and reinstall Docker Desktop to open it.

おすすめ

転載: blog.csdn.net/a6661314/article/details/128029017
おすすめ