我在AWS EC2上运行了一些docker容器,/var/lib/docker/overlay2文件夹的磁盘大小增长得非常快。

我想知道删除它的内容是否安全? 或者如果docker有某种命令来释放一些磁盘使用。


更新:

我实际上已经尝试了docker系统prune -a,它回收了0Kb。

此外,我的/docker/overlay2磁盘大小比docker系统df的输出大得多

在阅读docker文档和BMitch的回答后,我相信触摸这个文件夹是一个愚蠢的想法,我会尝试其他方法来回收我的磁盘空间。


当前回答

Docker使用/var/lib/docker来存储映像、容器和本地命名卷。删除它可能导致数据丢失,并可能停止引擎运行。overlay2子目录专门包含图像和容器的各种文件系统层。

要清除未使用的容器和映像,请参阅docker系统修剪。还有一些选项可以删除卷,甚至是带标签的图像,但默认情况下由于数据丢失的可能性而不启用:

$ docker system prune --help

Usage:  docker system prune [OPTIONS]

Remove unused data

Options:
  -a, --all             Remove all unused images not just dangling ones
      --filter filter   Provide filter values (e.g. 'label=<key>=<value>')
  -f, --force           Do not prompt for confirmation
      --volumes         Prune volumes

剪枝永远不会删除的内容包括:

正在运行的容器(使用docker ps列出它们) 这些容器上的日志(有关限制日志大小的详细信息,请参阅这篇文章) 由这些容器所做的文件系统更改(通过docker diff可见)

此外,在这个垃圾收集过程中,docker可能不会看到在普通docker文件夹之外创建的任何内容。这可能是其他应用写到这个目录,或者docker引擎之前的配置(例如,从AUFS切换到overlay2,或者可能在启用用户名称空间之后)。

What would happen if this advice is ignored and you deleted a single folder like overlay2 out from this filesystem? The container filesystems are assembled from a collection of filesystem layers, and the overlay2 folder is where docker is performing some of these mounts (you'll see them in the output of mount when a container is running). Deleting some of these when they are in use would delete chunks of the filesystem out from a running container, and likely break the ability to start a new container from an impacted image. See this question for one of many possible results.


为了完全刷新docker到一个干净的状态,你可以删除整个目录,而不仅仅是像overlay2这样的子目录:

# danger, read the entire text around this code before running
# you will lose data
sudo -s
systemctl stop docker
rm -rf /var/lib/docker
systemctl start docker
exit

引擎将在完全空的状态下重新启动,这意味着你将失去所有:

图片 容器 名叫卷 用户创建的网络 群状态

其他回答

也有快速增长的覆盖2的问题

/var/lib/docker/overlay2 -是docker存储容器可写层的文件夹。 只有当容器停止并删除时,Docker系统修剪-a才能工作。

在我的作品中,我能够通过研究overlay2来计算出什么占用了空间。

该文件夹包含其他命名为哈希的文件夹。每个都有几个文件夹,包括diff文件夹。

Diff文件夹——包含一个容器所写的实际差异,该容器具有与你的容器相同的文件夹结构(至少在我的情况下- ubuntu 18…)

所以我用du -hsc /var/lib/docker/overlay2/ longhashhhhhhh /diff/tmp来找出我的容器中的/tmp是被污染的文件夹。

因此,作为一个解决方案,我已经使用-v /tmp/container-data/tmp:/tmp参数为docker运行命令来映射内部/tmp文件夹到主机,并在主机上设置一个cron来清理该文件夹。

Cron任务很简单:

Sudo nano /etc/crontab */30 * * * * root rm -rf /tmp/container-data/tmp/* .使用实例 保存并退出

注意:overlay2是系统docker文件夹,他们可以随时改变它的结构。以上一切都是基于我在里面看到的。不得不进入docker文件夹结构,因为系统完全没有空间,甚至不允许我ssh进入docker容器。

我也有同样的问题,在我的实例中,这是因为' var/lib/docker '目录被挂载到一个运行的容器(在我的情况下谷歌/cadvisor),因此它阻止了docker修剪清理文件夹。停止容器,运行docker prune -然后重新运行容器解决了问题。

如果你的系统也用于构建映像,你可以看看如何清理由构建器创建的垃圾,使用:

docker buildx prune --all

and

docker builder prune --all

朋友们,为了保持一切清洁,你可以使用命令:

docker system prune -a && docker volume prune

不要在生产环境中这样做

@ravi-luthra给出的答案在技术上是有效的,但它有一些问题!

在我的例子中,我只是试图恢复磁盘空间。lib/docker/overlay文件夹占用了30GB的空间,我只定期运行几个容器。看起来docker有一些数据泄漏的问题,一些临时数据在容器停止时没有被清除。

所以我删除了lib/docker/overlay文件夹的所有内容。在那之后,我的docker实例变得不可用。当我试图运行或构建任何容器时,它给了我这个错误:

failed to create rwlayer: symlink ../04578d9f8e428b693174c6eb9a80111c907724cc22129761ce14a4c8cb4f1d7c/diff /var/lib/docker/overlay2/l/C3F33OLORAASNIYB3ZDATH2HJ7: no such file or directory

经过反复试验,我通过跑步解决了这个问题

(警告:这将删除docker卷内的所有数据)

docker system prune --volumes -a

所以不建议做这样的脏清理,除非你完全了解系统是如何工作的。