How do I clean up docker generated junk files

  • 2020-11-30 08:44:31
  • OfStack

I accidentally felt that I did not have enough disk space, so I used the ES1en-ES2en command to check, and the result was a surprise. It was all the good work of docker, which generated a heap of garbage in /var/lib/docker/aufs:


davidhopper@davidhopper-ThinkPad-P50s:~$ df -a
 The file system      1K- block     Has been used    available   Has been used %  The mount point 
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

Start by cleaning up all Docker containers that are not currently running, using the simplest method:


docker system prune

The operation results are as follows:


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? [y/N] y
Deleted Volumes:
333739e346364a7d515cdfc585f5231dd7f74a9e71431d152f6efc4da3bcb303
419184335b3c6130cb47b98c672e9666479ef0ff27cd2d20e173b55800507052
94165565a1f9d0553d18de62f86264596747f1b38c651b5e4f68391607e8453d
08d67461fa141609d0905f1209526131ce60864d1bf27650511a6bcc88197107
3aeb8f32513a1813f6b50726a233f16b20bb896b5cf3e51197c86e2cc10be374
3af830103bdedc7f035be9d2bdbd1104733c274f3ce629784cdf9daee8ac070e
78e7ab8000386d61050034a64eee041a2743b60c466598286f276534529db551
155c34c18faf8d234412a999b28d1d6de8517b68eac5dc47e58964a0efb24a06
200d23a03f40cb1b8af8e5368805370d45195448fbad0944bcb9730f2140d462
51b2e0825e25de4c42475245a83b2119ce45e75da811ad6df1525f7ff2888a36
559532c3877cf5641e21c15fca6d923266b6548a4aef834667b0c66ca78cde38
a05cf184006b1301eb91e2ebcb9cca6d5868265f56bdac0aefd5228979fd8c74
c21d336b8205ea12e27414c3a00eef6a329222ea7552b825d3889ef042783820
3e573f8d84fd7923415b46a90017ff5cfeb2bd2931e4ec54c45c4bd62664042d
3ee39b3a5e49b605d4069e99c4c1d01db6a210d28cbdc350672b33accd2b0f3b
a0fe3e6d80b6fb4dc6ca2159789e512912fb4ffccd0df1183bafc55c4e804563
3e062023110a4c13e7915da03ffaad3dbaa558c590a6b2274c2fb09ad54abd6c
69276b6274956a28abfe50fdf459a0042ecba6978e379e035399c9272c930b57
de1a0669765d765699b47999c59ed74f93fc0545fe8d280cefdd2cf9e2b31379
33766d8c693b826d9da48d419c15b90fcfbac26eb9939749aef94218a31a767b
45a2047d0581ad07ebd0164e69897f5ae99144a4f0422b72f184281ae47e0b4d
7191a9c6cea03c4aa1aac1c742228c46e3d7e2577645a037d62600be7249a649
7b45e555bca1d941af284743396753199f7bea33e7bf3f3362f385ec074b4450
08cb6f6b2c1d83796e9bcdd6eb6d2930ba80f98721ed1215d9bd6c75148a2608
0a786b212dc4e76225ae3e90dfd92c6eba5a4f60f465d86e62990fe22311ba1c
2a50f3203d93d60e115ec3b3ba643b3213f98d3c793730332de0178a9f7b5c45
3b50fae52487f178325d0347f75eb2502195daf5e39ba156a37752d3d62cd915
9067fa92cee1da6d1044ce51d616dc0cad67ac31e16f4f208ba840599dcb0b21
cb07e29eabcbb62bbcbe13443267a96599747ca52b685a71b7233cf94d876d38
67c55c6079ff257be64a59ee83d08c463591aebd5496dd1f134c33205d9f5c12
0a994bec4d112f6b012b7be4f5b7ad25c557c01f4392cf02546eceb697913240
90d85444a338c247414adc209e42a46c89d30cc9c1c52d29c0d63a5e35b5d70c
ec2bb3abb42e7450005e9c9b30305a5763e44dc03a70f313062d57ef8062b10a
09d99b0fd826d7463c1816228953c0492107916283de2b1ccb7fa8e22c2de324
1ad2def94eaa92707b9cf3b7ef288b57c64cff0094103f776657aaeacae8c939
92f633c27c6796091311e83617d4fb3b1526689b5b6ada33f58214ffa80c1f5e
053bacc4b3bf55149f918368238ffe50c1ed777558092b7b883b2cda2b7a0313
15118045d54acb0e63c0ebd4460a1a1666b3c930b95d351a1b831419eb3f2226
2852c53d5b177522c67ffd9d916d66328ede26275c27a80745812b1c28b8d47f
abe49f612dc9e8250f464772c726850415a051562ffcd922dc796a71bb94d061
6927af521e03ec75a09dbfe0a86bbf69928773841c538a2fc094bc4368bd3138
6d97a92956067706ba5d8ac9d01e99294d8ca28e4182a81c0662f77faf8e372b
8496afb1946e3f5863a23c8394f4065293e26dbc2555bf474d0328f56a613331
829e41a7d593b75633868c72bb81123d32f2a14b656f1409c57da6bc984fa233
9205da799d47bc613bf7b1955ea78712f16b4edf2a79b450f55ffb6111dee609
923a72e6bde7fb13fae29995611f9d28b0bdd4cf6a53250c45006edbe795d120
e31e6d693398b033843b0ad9cfceec09b9e1b47f7a9f4b2bec275e5e6a23fa3b
0163e1b3e1d90e20410e33b5d95eec75b55819c1117fdd57c655c31ed589f524
479c0acef02c7b1ddcb5863198868f71b6b2f3275146de73f9c884a9f50dd32e
7a231567bbc73d11621d34cdacc45061cfe060e78210782e18fec100578fe56a

Total reclaimed space: 26.96GB

If have enough space to release, to find a way to remove/var/lib/docker/aufs folder, the most simple idea is to delete directly:


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

The results were disappointing, with the following prompt:


davidhopper@davidhopper-ThinkPad-P50s:~$ sudo rm -rf /var/lib/docker/aufs
[sudo] davidhopper  Password:  
rm:  Unable to delete '/var/lib/docker/aufs':  Equipment or resources are busy 

With the command cat /proc/mounts | grep "docker" Check the loading condition of the device, and sure enough, there is 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

There is no way, forcing me to use the trick, first load the device, then delete it:


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

Using the ES36en-ES37en command again, the results are refreshing:


davidhopper@davidhopper-ThinkPad-P50s:~$ df -a
 The file system      1K- block     Has been used     available   Has been used %  The mount point 
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

In order for docker to take up no more space in the future, we specify the maximum space for the docker directory with the following command:


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

The maximum size of the file created is 1MB * 1024 = 10 GB

Note:

If Docker becomes unusable due to the deletion of the junk file, you can use the following method to delete all the relevant files and then reinstall Docker. The relevant files can be deleted as follows:

1. Find the Docker cache file location


sudo find / -name "docker"

The results on my machine are as follows:


docker system prune
0

2. Delete the Docker cache

sudo rm -rf /home/davidhopper/.cache/bazel/_bazel_davidhopper/install/5b1e8be5d1d6766f635a7db5967707e9/_embedded_binaries/embedded_tools/tools/build_defs/docker


Related articles: