简体   繁体   English

为什么删除 docker 容器和图像不会释放 Windows 上的存储空间? 如何解决?

[英]Why removing docker containers and images does not free up storage space on Windows? How to fix it?

Problem:问题:

As I download and run containers, they keep taking up more and more space.当我下载并运行容器时,它们会占用越来越多的空间。 I found suggestions for cleaning up unused containers and images and I did so.我找到了清理未使用的容器和图像的建议,我这样做了。 Guess what?你猜怎么着? They eat up even more disk space他们吃掉了更多的磁盘空间

What I found so far:到目前为止我发现了什么:

It has to do with .docker\machine\machines\default\disk.vmdk file.它与.docker\machine\machines\default\disk.vmdk文件有关。 It only gets bigger它只会变得更大

Log of disk.vmdk: disk.vmdk 的日志:

                            size (MB)
1. with 2 images                1,376
2. downloading a new image X    ?
3. running X as Y               2,963
4. removing Y                   2,963
5. removing X                   3,106
6. removing all the images      3,126

The only fix I found so far was running docker-machine rm default which removes the VM.到目前为止,我发现的唯一修复是运行docker-machine rm default来删除 VM。 The problem is that I have to download all the images again.问题是我必须再次下载所有图像。 There should be a better fix.应该有更好的解决办法。 Can someone explain:有人可以解释一下:

  1. What is going on?到底是怎么回事?
  2. How to fix it?如何解决?

There are maintainance commands you can run on the more recent versions of Docker.您可以在较新版本的 Docker 上运行维护命令。 They will free up space used by stopped containers, dangling images and dangling volumes:它们将释放被停止容器、悬空图像和悬空卷使用的空间:

docker container prune -f
docker image prune -f
docker volume prune -f

Maybe the images you are using, use volumes.也许您正在使用的图像,使用卷。 If they do then deleting the container doesn't do the trick.如果他们这样做,那么删除容器并不能解决问题。 You must delete the volumes as well.您还必须删除卷。 In order to do that you must specify the -v flag when deleting a container为此,您必须在删除容器时指定-v标志

docker rm -v <container name or container id>

Depending on your docker version you will have some more commands available.根据您的 docker 版本,您将有更多可用的命令。 Check this SO thread for more.查看此SO 线程了解更多信息。 You can read more about orphaned volumes in this SO thread您可以在此SO 线程中阅读有关孤立卷的更多信息

After deleting all the unwanted containers and images using the commands below:使用以下命令删除所有不需要的容器和图像后:

  • Docker container rm Docker 容器 rm
  • Docker image rm Docker 镜像 rm
  • Docker container prune Docker 容器修剪
  • Docker image prune Docker 镜像修剪
  • Docker volume prune Docker 卷修剪

I couldn't see any disk space released.我看不到任何释放的磁盘空间。 I found out the disk space was released after I restarted my Docker desktop.我发现重新启动 Docker 桌面后磁盘空间已释放。

On windows 10, this was a major issue, space was not freeing up, even after I ran docker system prune I started noticing this when I found that every week;在 Windows 10 上,这是一个主要问题,空间没有释放,即使在我运行docker system prune之后,当我每周都发现时,我开始注意到这一点; my SSD was filling up every2 or 3 GB of space.我的 SSD 每 2 或 3 GB 空间就被填满了。

Try opening Docker Desktop, hitting the Troubleshoot icon at the top right of the UI, and then clicking "Clean / Purge data".尝试打开 Docker 桌面,点击 UI 右上角的疑难解答图标,然后单击“清理/清除数据”。 This reclaimed the space for me.这为我收回了空间。

Also see:另见:

  1. Windows 10: Docker does not release disk space after deleting all images and containers #244 Windows 10:Docker 在删除所有镜像和容器后不释放磁盘空间 #244
  2. Hyper V ui issue fix Hyper V ui 问题修复
  3. Clear Hyper-V recognize unused sectors 清除 Hyper-V 识别未使用的扇区
  4. Linux growing docker size issue fix Linux 不断增长的 docker 大小问题修复

If anyone is struggling with this problem on Ubuntu:如果有人在 Ubuntu 上遇到这个问题:

In my case pruning and removing using the docker command did not help much.就我而言,使用 docker 命令进行修剪和删除并没有多大帮助。

The issue was docker/overlay2 folder.问题是docker/overlay2文件夹。

febrin@laptop:/var/lib$ sudo du -sh docker/overlay2
361G    docker/overlay2

I had to delete it manually.我不得不手动删除它。

On windows using wsl2 follow the below steps.在使用 wsl2 的 windows 上,请按照以下步骤操作。 Note that you will lose all the docker images and containers if you follow this.请注意,如果您按照此操作,您将丢失所有 docker 图像和容器。 If you want to retain data(images and containers) then you can try the prune option mentioned by other people on this thread.如果您想保留数据(图像和容器),那么您可以尝试此线程上其他人提到的修剪选项。

Locate the vhdx file where docker stores the data.找到 docker 存储数据的 vhdx 文件。 This is a virtual hard disk used by docker.这是docker使用的虚拟硬盘。

"P:\Users\your-username\AppData\Local\Docker\wsl\data\ext4.vhdx"

In my case docker is using up about 27GB of disk space in this file.在我的例子中,docker 在这个文件中使用了大约 27GB 的磁盘空间。 Even after deleting all the images and container, docker is not releasing the free disk space back to OS.即使在删除所有图像和容器之后,docker 也不会将可用磁盘空间释放回操作系统。 在此处输入图像描述

To reclaim the disk space, you have to try clean/purge data option from the GUI.要回收磁盘空间,您必须尝试从 GUI 中清除/清除数据选项。 When prompted for the data set name, select WSL 2. This should clean up and give back all the unused space.当提示输入数据集名称时,select WSL 2。这应该清理并归还所有未使用的空间。

In case, the docker daemon doesn't start up or disk space is not reduced then the last option is to factory restore from docker desktop GUI like show below.如果 docker 守护程序未启动或磁盘空间未减少,则最后一个选项是从 docker 桌面 GUI 恢复出厂设置,如下所示。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker for Windows如何运行Linux容器? - How does Docker for Windows run Linux containers? Windows 10 上的 Docker:如何将现有容器和图像移动到新位置? - Docker on Windows 10: How to move existing containers & images to new location? In Docker Desktop for windows 10 with WSL2, where does docker containers live &amp; how Linux containers can run a java app, but not windows nanoserver? - In Docker Desktop for windows 10 with WSL2, where does docker containers live & how Linux containers can run a java app, but not windows nanoserver? Docker 镜像和容器位置与 Windows 10 使用 hyper-v - Docker images and containers location with Windows 10 using hyper-v docker 和Windows一起使用时,镜像和容器存储在哪里? - Where are docker images and containers stored when we use it with Windows? Docker windows 容器中的塑料 - Plastic in Docker windows containers 如何使用 Windows 的 Docker 为 Windows 容器配置 DNS? - How to configure DNS for Windows containers using Docker for Windows? Windows版Docker:在本地使用相同的端口号启动多个Docker容器 - Docker for Windows: Spinning up multiple docker containers locally with same port numbers 如何将 docker 容器归档到 windows 容器中 - How to files form docker container to host in windows containers "Docker for Windows 容器环境变量" - Docker for Windows Containers environment variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM