查看 Docker 容器目录大小

/var/lib/docker/ 占用的空间很大,但不知到底是哪个容器占用的多,这个工具的作用就是把每个容器占用的空间展示出来

#!/bin/bash

dockerdir=/var/lib/docker  
volumesdir=${dockerdir}/volumes  
vfsdir=${dockerdir}/vfs/dir


container_ids=$(docker ps -a -q --no-trunc)

for container in $container_ids; do  
        name=$(docker inspect -f '{{ .Name }}' $container)
        echo ' '
        echo ' '
        echo '----------------  Docker Container Volumes  --------------------'
        echo 'Container NAMES =' $name
        echo 'Container ID =' $container
        echo 'Container Volumes'


        for volpath in $(
                docker inspect --format='{{range $key, $val := .}}{{if eq $key "Volumes"}}{{range $vol, $path := .}}{{$path}}{{"\n"}}{{end}}{{end}}{{if eq $key "Mounts"}}{{range $mount := $val}}{{$mount.Source}}{{"\n"}}{{end}}{{end}}{{end}}' ${container} \
        ); do
                vid=$(echo "${volpath}" | sed 's|.*/\(.*\)/_data$|\1|;s|.*/\([0-9a-f]\{64\}\)$|\1|')
                du -sh $vfsdir/$vid

        done
done