清理日志:

如下目录是docker的日志文件:有时会很大达到几十个G。

cd /var/lib/docker/containers
06f1bdf54fbad730ed5ea8401a2137f52f0b1029875525233e2b0aeae22a22e1-json.log

清理即可:如果容器在运行期间不可直接删除,而只能清空文件。

cat /dev/null > 06f1bdf54fbad730ed5ea8401a2137f52f0b1029875525233e2b0aeae22a22e1-json.log

限制docker日志文件大小:

max-file: "3",意味着一个容器有三个日志,分别是id+.json、id+1.json、id+2.json

max-size: "1g",滚动前日志的最大大小。一个正整数加上一个代表测量单位(k,m 或 g)的修饰符。默认为 -1(无限制)。

vim /etc/docker/daemon.json
{
  "log-driver":"json-file",
  "log-opts": {"max-size":"20m", "max-file":"3"}
}

重启docker:

systemctl daemon-reload
systemctl restart docker


清理其他空间:

查看docker占用的磁盘空间:

$docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              33                  33                  6.363GB             669.6MB (10%)
Containers          74                  49                  -74B                -25B
Local Volumes       7                   0                   12.68kB             12.68kB (100%)
Build Cache                                                 0B                  0B


$docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              33                  18                  6.363GB             3.821GB (60%)
Containers          49                  49                  -49B                0B
Local Volumes       7                   0                   12.68kB             12.68kB (100%)
Build Cache                                                 0B                  0B

清理:

docker system prune
docker system prune -a # 暂时关闭的容器镜像也会删除

参考:

blog.fundebug.com/2018/01/10/how-to-clean-docker-disk/


定时任务清理:

#!/bin/bash

export DIR=/var/lib/docker/containers
export BIG_DIR_LIST=`du -sh $DIR/* | grep G| awk '{print $2}'`


for var in $BIG_DIR_LIST
do
    log_dir_name=`basename $var`
    big_log_file=$DIR/$log_dir_name/$log_dir_name'-json.log'
    if [ -f $big_log_file ]; then
        cat /dev/null > $big_log_file
        echo "cat /dev/null > $big_log_file" >> /tmp/clear.log
    fi
done


设置文件系统类型:

vim /etc/docker/daemon.json 
{
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ]
}