docker delete image file

There are too many images in the docker container, and the D disk is full


4 View and remove mirror images

1 view mirror

docker images

2 remove mirror command

docker rmi '镜像名称'   # 只输入前四位即可

Five practical and effective operations

  • Clear all unused resources
docker system prune

This command will delete all unused resources such as images, containers, and data volumes.

  • Clear all unmarked mirrors
docker image prune -a

This command will delete all untagged images.

  • Clear all unused data volumes
docker volume prune

This command will delete all unused data volumes.

  • Clear all unused networks
docker network prune

This command will delete all unused networks.

Six View Container Occupancy

docker system df # 【查看docker容器占用命令】

7. For the situation where the data volume is still not cleaned up after deleting all mirrors

Reference: [Solved] WSL2 disk space is not released after Windows docker deletes the container

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 be automatically reduced. Once there are a lot of 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.

1 location found

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 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

3 Compress the virtual disk file

Execute in PowerShell:

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

A new command window called DiskPart will open

Execute in the newly opened DiskPart command window:

# 选择虚拟磁盘文件
select vdisk file="就是步骤7.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.

Guess you like

Origin blog.csdn.net/March_A/article/details/132166449