gpt4 book ai didi

docker - 为什么长时间运行的 docker 实例会填满我的磁盘空间?

转载 作者:行者123 更新时间:2023-12-04 13:52:51 29 4
gpt4 key购买 nike

当我启动一台新的 Ubuntu 机器 (EC2) 并下载一个运行了很长时间的 docker 镜像时,几周后磁盘就会填满。我该如何防止这种情况发生?
我在网上找到的所有内容都在谈论运行 docker prune,但我的问题与大量杂散的 docker 图像或周围的卷无关。此 EC2 实例下载单个镜像并仅启动一次(并使其永远运行,这是一个 CI 运行器)。
这里有一些线索:

  • 主机和 docker 镜像都是 Ubuntu 20.04
  • 我的 EC2 实例有一个 10 GB 的卷
  • 当我docker pull图像只有 2.5 GB(这是一个 ubuntu 最小图像)
  • 引导脚本使用以下命令启动 docker:
  • docker run -it -d --rm --shm-size=2gb --env --user root --name running-docker-ci ghcr.io/secret/docker-ci:latest start这是我做过的诊断:
    $ df
    Filesystem 1K-blocks Used Available Use% Mounted on
    /dev/root 10098432 10082048 0 100% /
    devtmpfs 8192212 0 8192212 0% /dev
    tmpfs 8198028 0 8198028 0% /dev/shm
    tmpfs 1639608 164876 1474732 11% /run
    tmpfs 5120 0 5120 0% /run/lock
    tmpfs 8198028 0 8198028 0% /sys/fs/cgroup
    /dev/loop0 34176 34176 0 100% /snap/amazon-ssm-agent/3552
    /dev/loop1 56832 56832 0 100% /snap/core18/1988
    /dev/loop4 33152 33152 0 100% /snap/snapd/11588
    /dev/loop5 56832 56832 0 100% /snap/core18/1997
    /dev/loop6 72192 72192 0 100% /snap/lxd/19647
    /dev/loop7 69248 69248 0 100% /snap/lxd/20326
    /dev/loop2 32896 32896 0 100% /snap/snapd/11841
    tmpfs 1639604 0 1639604 0% /run/user/1000
    并运行 du很多使我成为我最大的文件夹:
    /var/lib/docker$ sudo du -s * | sort -nr | head -50
    13842100 overlay2
    14888 image
    128 containers
    72 buildkit
    56 network
    28 volumes
    20 plugins
    20 builder
    4 trust
    4 tmp
    4 swarm
    4 runtimes
    有什么帮助吗?我难住了。

    添加更多详细信息:
    larsks 建议可能这是在容器内。它似乎不是。我没有运行任何生成文件的东西。奇怪的是,我注意到 df显示 overlay 使用了 8 个演出文件系统:
    $ df
    Filesystem 1K-blocks Used Available Use% Mounted on
    overlay 8065444 8049060 0 100% /
    tmpfs 65536 0 65536 0% /dev
    tmpfs 8198028 0 8198028 0% /sys/fs/cgroup
    shm 2097152 16 2097136 1% /dev/shm
    /dev/root 8065444 8049060 0 100% /etc/hosts
    tmpfs 8198028 0 8198028 0% /proc/acpi
    tmpfs 8198028 0 8198028 0% /proc/scsi
    tmpfs 8198028 0 8198028 0% /sys/firmware
    但是什么时候做 du在目录树上,它不会加起来接近 8 个演出。我从正在运行的容器内的文件系统的根目录运行它:
    $ sudo du -s * | sort -nr | head -50

    3945724 home
    1094712 usr
    254652 opt
    151984 var
    3080 etc
    252 run
    192 tmp
    24 root
    16 dev
    4 srv
    4 mnt
    4 media
    4 boot
    0 sys
    0 sbin
    0 proc
    0 libx32
    0 lib64
    0 lib32
    0 lib
    0 bin

    最佳答案

    OverlayFS 的部分工作原理似乎是删除操作并不总是释放文件系统中的空间。来自 the docs :

    • Deleting files and directories:

    When a file is deleted within a container, a whiteout file is created in the container (upperdir). The version of the file in the image layer (lowerdir) is not deleted (because the lowerdir is read-only). However, the whiteout file prevents it from being available to the container.


    When a directory is deleted within a container, an opaque directory is created within the container (upperdir). This works in the same way as a whiteout file and effectively prevents the directory from being accessed, even though it still exists in the image (lowerdir).


    在不了解您的 CI 程序的情况下,很难准确地说出,但重点仍然是,如果您认为您正在删除文件,则文件系统很可能保留了它们的部分或全部内容。
    顺便说一句,既然您提到您在 AWS 上,您可能会考虑使用无服务器 CI 部署,以便您的容器在每次运行时都从零开始。

    关于docker - 为什么长时间运行的 docker 实例会填满我的磁盘空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67523890/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com