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

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


更新:

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

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

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


当前回答

基于Mert Mertce的回答,我编写了以下脚本,包括旋转器和进度条。

然而,自从写了脚本之后,我注意到我们构建服务器上的额外目录是暂时的——也就是说,Docker似乎正在清理,尽管速度很慢。我不知道如果存在删除目录的竞争,Docker是否会感到不安。我们目前的解决方案是使用documentum,但是有很多额外的开销(150+GB)。

#!/bin/bash
[[ $(id -u) -eq 0 ]] || exec sudo /bin/bash -c "$(printf '%q ' "$BASH_SOURCE" "$@")"
progname=$(basename $0)
quiet=false
no_dry_run=false
while getopts ":qn" opt
do
    case "$opt" in
      q)
          quiet=true
          ;;
      n)
          no_dry_run=true
          ;;
      ?)
          echo "unexpected option ${opt}"
          echo "usage: ${progname} [-q|--quiet]"
          echo "    -q: no output"
          echo "    -n: no dry run (will remove unused directories)"
          exit 1
          ;;
    esac
done
shift "$(($OPTIND -1))"

[[ ${quiet} = false ]] || exec /bin/bash -c "$(printf '%q ' "$BASH_SOURCE" "$@")" > /dev/null

echo "Running as: $(id -un)"

progress_bar() {
    local w=80 p=$1;  shift
    # create a string of spaces, then change them to dots
    printf -v dots "%*s" "$(( $p*$w/100 ))" ""; dots=${dots// /.};
    # print those dots on a fixed-width space plus the percentage etc.
    printf "\r\e[K|%-*s| %3d %% %s" "$w" "$dots" "$p" "$*";
}

cd /var/lib/docker/overlay2
echo cleaning in ${PWD}
i=1
spi=1
sp="/-\|"
directories=( $(find . -mindepth 1 -maxdepth 1 -type d | cut -d/ -f2) )
images=( $(docker image ls --all --format "{{.ID}}") )
total=$((${#directories[@]} * ${#images[@]}))
used=()
for d in "${directories[@]}"
do
    for id in ${images[@]}
    do
        ((++i))
        progress_bar "$(( ${i} * 100 / ${total}))" "scanning for used directories ${sp:spi++%${#sp}:1} "
        docker inspect $id | grep -q $d
        if [ $? ]
        then
            used+=("$d")
            i=$(( $i + $(( ${#images[@]} - $(( $i % ${#images[@]} )) )) ))
            break
        fi
    done
done
echo -e "\b\b " # get rid of spinner
i=1
used=($(printf '%s\n' "${used[@]}" | sort -u))
unused=( $(find . -mindepth 1 -maxdepth 1 -type d | cut -d/ -f2) )
for d in "${used[@]}"
do
    ((++i))
    progress_bar "$(( ${i} * 100 / ${#used[@]}))" "scanning for unused directories ${sp:spi++%${#sp}:1} "
    for uni in "${!unused[@]}"
    do
        if [[ ${unused[uni]} = $d ]]
        then
            unset 'unused[uni]'
            break;
        fi
    done
done
echo -e "\b\b " # get rid of spinner
if [ ${#unused[@]} -gt 0 ]
then
    [[ ${no_dry_run} = true ]] || echo "Could remove:  (to automatically remove, use the -n, "'"'"no-dry-run"'"'" flag)"
    for d in "${unused[@]}"
    do
        if [[ ${no_dry_run} = true ]]
        then
            echo "Removing $(realpath ${d})"
            rm -rf ${d}
        else
            echo " $(realpath ${d})"
        fi
    done
    echo Done
else
    echo "All directories are used, nothing to clean up."
fi

其他回答

在上面的评论中,人们建议修剪系统,如清除悬空卷,图像,退出容器等,有时你的应用程序成为罪魁祸首,它在很短的时间内产生了太多的日志,如果你使用一个空的目录卷(本地卷),这会填满/var分区。在这种情况下,我发现下面的命令非常有趣,要弄清楚是什么在消耗我的/var分区磁盘上的空间。

du -ahx /var/lib | sort -rh | head -n 30

This command will list top 30, which is consuming most space on a single disk. Means if you are using external storage with your containers, it consumes a lot of time to run du command. This command will not count mount volumes. And is much faster. You will get the exact directories/files which are consuming space. Then you can go to those directories and check which files are useful or not. if these files are required then you can move them to some persistent storage by making change in app to use persistent storage for that location or change location of that files. And for rest you can clear them.

我发现这个方法最适合我:

docker image prune --all

默认情况下,Docker不会删除已命名的图像,即使它们未使用。该命令将删除未使用的映像。

注意,图像中的每一层都是/usr/lib/docker/overlay2/文件夹中的一个文件夹。

我有这样的问题…那是一根巨大的木头。日志如下:

/var/lib/docker/containers/<container id>/<container id>-json.log

您可以在运行命令行或撰写文件中进行管理。参见:配置日志记录驱动程序

我个人将这3行添加到我的docker-compose中。Yml文件:

my_container:
  logging:
    options:
      max-size: 10m

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

/var/lib/docker中的所有内容都是容器的文件系统。如果你停止所有的容器并修剪它们,你应该以文件夹为空结束。你可能并不想那样做,所以不要随意删除里面的内容。不要直接删除/var/lib/docker中的内容。有时你可能会侥幸逃脱,但出于很多原因,这是不可取的。

你可以这样做:

sudo bash
cd /var/lib/docker
find . -type f | xargs du -b  | sort -n

您将看到的是底部显示的最大文件。如果你想,找出这些文件在什么容器里,用docker exec -ti containername——/bin/sh输入这些容器,然后删除一些文件。

你也可以把docker系统prune -a -f放在每日/每周的cron作业中,只要你不留下你所关心的停止的容器和卷。最好是找出它增长的原因,并在容器级别上纠正它们。