站长资源服务器

如何清理docker产生的垃圾文件

整理:jimmy2024/12/29浏览2
简介无意中感觉磁盘空间不够用,于是使用df -a命令查看,结果令人大吃一惊,全是docker干的好事,在/var/lib/docker/aufs里生成了一堆垃圾:davidhopper@davidhopper-ThinkPad-P50s:~$ df -a文件系统 1K-块 已用 可用 已

无意中感觉磁盘空间不够用,于是使用df -a命令查看,结果令人大吃一惊,全是docker干的好事,在/var/lib/docker/aufs里生成了一堆垃圾:

davidhopper@davidhopper-ThinkPad-P50s:~$ df -a
文件系统     1K-块   已用  可用 已用% 挂载点
sysfs         0    0    0   - /sys
proc         0    0    0   - /proc
udev      8112832    0 8112832  0% /dev
devpts        0    0    0   - /dev/pts
tmpfs      1627828   9580 1618248  1% /run
/dev/sda6   84293120 77948404 2039772  98% /
securityfs      0    0    0   - /sys/kernel/security
tmpfs      8139124  18040 8121084  1% /dev/shm
tmpfs       5120    4  5116  1% /run/lock
tmpfs      8139124    0 8139124  0% /sys/fs/cgroup
cgroup        0    0    0   - /sys/fs/cgroup/systemd
pstore        0    0    0   - /sys/fs/pstore
efivarfs       0    0    0   - /sys/firmware/efi/efivars
cgroup        0    0    0   - /sys/fs/cgroup/net_cls,net_prio
cgroup        0    0    0   - /sys/fs/cgroup/perf_event
cgroup        0    0    0   - /sys/fs/cgroup/cpu,cpuacct
cgroup        0    0    0   - /sys/fs/cgroup/pids
cgroup        0    0    0   - /sys/fs/cgroup/devices
cgroup        0    0    0   - /sys/fs/cgroup/cpuset
cgroup        0    0    0   - /sys/fs/cgroup/rdma
cgroup        0    0    0   - /sys/fs/cgroup/blkio
cgroup        0    0    0   - /sys/fs/cgroup/hugetlb
cgroup        0    0    0   - /sys/fs/cgroup/freezer
cgroup        0    0    0   - /sys/fs/cgroup/memory
systemd-1       -    -    -   - /proc/sys/fs/binfmt_misc
debugfs        0    0    0   - /sys/kernel/debug
mqueue        0    0    0   - /dev/mqueue
hugetlbfs       0    0    0   - /dev/hugepages
fusectl        0    0    0   - /sys/fs/fuse/connections
configfs       0    0    0   - /sys/kernel/config
/dev/sda1    262144  33084 229060  13% /boot/efi
binfmt_misc      0    0    0   - /proc/sys/fs/binfmt_misc
tmpfs      1627828    56 1627772  1% /run/user/1000
gvfsd-fuse      0    0    0   - /run/user/1000/gvfs
/dev/sda6   84293120 77948404 2039772  98% /var/lib/docker/aufs

首先使用最简单的方法,清理当前未运行的所有Docker容器:

docker system prune

运行结果如下:

davidhopper@davidhopper-ThinkPad-P50s:~/code/apollo$ docker system prune
WARNING! This will remove:
  - all stopped containers
  - all volumes not used by at least one container
  - all networks not used by at least one container
  - all dangling images
Are you sure you want to continue"htmlcode">
sudo rm -rf /var/lib/docker/aufs

结果令人失望,提示信息如下:

davidhopper@davidhopper-ThinkPad-P50s:~$ sudo rm -rf /var/lib/docker/aufs
[sudo] davidhopper 的密码: 
rm: 无法删除'/var/lib/docker/aufs': 设备或资源忙

用命令cat /proc/mounts | grep "docker"查找设备加载情况,果然有aufs:

davidhopper@davidhopper-ThinkPad-P50s:~$ cat /proc/mounts | grep "docker"
/dev/sda6 /var/lib/docker/aufs ext4 rw,relatime,errors=remount-ro,data=ordered 0 0

没办法,逼我用绝招,先缷载设备,再删除之:

sudo umount /var/lib/docker/aufs
sudo rm -rf /var/lib/docker/aufs

再次使用df -a命令查看,结果令人神清气爽:

davidhopper@davidhopper-ThinkPad-P50s:~$ df -a
文件系统     1K-块   已用   可用 已用% 挂载点
sysfs         0    0    0   - /sys
proc         0    0    0   - /proc
udev      8112832    0 8112832  0% /dev
devpts        0    0    0   - /dev/pts
tmpfs      1627828   9584 1618244  1% /run
/dev/sda6   84293120 65296764 14691412  82% /
securityfs      0    0    0   - /sys/kernel/security
tmpfs      8139124  18256 8120868  1% /dev/shm
tmpfs       5120    4   5116  1% /run/lock
tmpfs      8139124    0 8139124  0% /sys/fs/cgroup
cgroup        0    0    0   - /sys/fs/cgroup/systemd
pstore        0    0    0   - /sys/fs/pstore
efivarfs       0    0    0   - /sys/firmware/efi/efivars
cgroup        0    0    0   - /sys/fs/cgroup/net_cls,net_prio
cgroup        0    0    0   - /sys/fs/cgroup/perf_event
cgroup        0    0    0   - /sys/fs/cgroup/cpu,cpuacct
cgroup        0    0    0   - /sys/fs/cgroup/pids
cgroup        0    0    0   - /sys/fs/cgroup/devices
cgroup        0    0    0   - /sys/fs/cgroup/cpuset
cgroup        0    0    0   - /sys/fs/cgroup/rdma
cgroup        0    0    0   - /sys/fs/cgroup/blkio
cgroup        0    0    0   - /sys/fs/cgroup/hugetlb
cgroup        0    0    0   - /sys/fs/cgroup/freezer
cgroup        0    0    0   - /sys/fs/cgroup/memory
systemd-1       -    -    -   - /proc/sys/fs/binfmt_misc
debugfs        0    0    0   - /sys/kernel/debug
mqueue        0    0    0   - /dev/mqueue
hugetlbfs       0    0    0   - /dev/hugepages
fusectl        0    0    0   - /sys/fs/fuse/connections
configfs       0    0    0   - /sys/kernel/config
/dev/sda1    262144  33084  229060  13% /boot/efi
binfmt_misc      0    0    0   - /proc/sys/fs/binfmt_misc
tmpfs      1627828    52 1627776  1% /run/user/1000
gvfsd-fuse      0    0    0   - /run/user/1000/gvfs

为了让docker以后不再占用更多空间,我们指定docker目录的最大空间,命令如下:

mkdir -p /var/lib/docker/devicemapper/devicemapper 
dd if=/dev/zero of=/var/lib/docker/devicemapper/devicemapper/data bs=1M count=0 seek=1024

建立的文件最大尺寸为 1MB * 1024 = 10 GB

注意:

如果删除垃圾文件后,导致Docker无法使用,则可使用如下方法将相关文件全部删除,然后重新安装Docker。删除相关文件的方法如下:

1.查找Docker缓存文件位置

sudo find / -name "docker"

在我机器上结果如下:

/sys/fs/cgroup/memory/docker
/sys/fs/cgroup/blkio/docker
/sys/fs/cgroup/hugetlb/docker
/sys/fs/cgroup/pids/docker
/sys/fs/cgroup/cpuset/docker
/sys/fs/cgroup/perf_event/docker
/sys/fs/cgroup/devices/docker
/sys/fs/cgroup/net_cls,net_prio/docker
/sys/fs/cgroup/freezer/docker
/sys/fs/cgroup/cpu,cpuacct/docker
/sys/fs/cgroup/systemd/docker
/etc/default/docker
/etc/docker
/etc/init.d/docker
/var/lib/docker
/run/docker
find: `/run/user/1000/gvfs': 权限不够
/usr/lib/docker
/usr/share/code/resources/app/extensions/docker
/usr/share/bash-completion/completions/docker
/usr/bin/docker
/home/davidhopper/Qt5.10.0/5.10.0/Src/qtwebengine/src/3rdparty/chromium/third_party/protobuf/jenkins/docker
/home/davidhopper/.cache/bazel/_bazel_davidhopper/install/5b1e8be5d1d6766f635a7db5967707e9/_embedded_binaries/embedded_tools/tools/build_defs/docker
/home/davidhopper/.vscode/extensions/njpwerner.autodocstring-0.2.0/.vscode-test/Visual Studio Code.app/Contents/Resources/app/extensions/docker
/home/davidhopper/code/apollo/docker
/home/davidhopper/code/protobuf-master/jenkins/docker
/home/davidhopper/code/apollo1215/apollo/docker

2.删除Docker缓存

复制代码 代码如下:sudo rm -rf /home/davidhopper/.cache/bazel/_bazel_davidhopper/install/5b1e8be5d1d6766f635a7db5967707e9/_embedded_binaries/embedded_tools/tools/build_defs/docker

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。