Docker目录/var/lib/docker/containers文件太大

半兽人 发表于: 2020-07-20   最后更新时间: 2021-03-02 15:44:37  
{{totalSubscript}} 订阅, 7,550 游览

Docker在不重建容器的情况下,日志文件默认会一直追加,时间一长会逐渐占满服务器的硬盘的空间,内存消耗也会一直增加,本篇来了解一些控制日志文件的方法。

查出占用磁盘较大的文件

Docker 的日志文件存在 /var/lib/docker/containers 目录中,通过下面的命令可以将日志文件夹根据升序的方式罗列出来。

sudo du -d1 -h /var/lib/docker/containers | sort -h

40K     /var/lib/docker/containers/7bbec35275e8f63784107d486c9c40cb62902afd6815c569b36947f0b0ed6405
40K     /var/lib/docker/containers/7df78d67728173c13b4a238518b79d4b4a384d38026102a61aef290e53f2f1a6
40K     /var/lib/docker/containers/8d59b4f970a1b5cb20fc281d73f5c643bd0514b25cac3e0fae13bcec99c45e05
40K     /var/lib/docker/containers/965375d69bca2e43a4f7d3306508cc1db40fa0b783b1d7fe4b6fa7e333926b8c
40K     /var/lib/docker/containers/e04e90f0d466ab5b969fd72894b23444d495a84591b1e93d5bef03da30b1fc2d
40K     /var/lib/docker/containers/f31069b69d329fcac0939bbb6310afa66535ff4573591414fbfabecdb0eeeb79
44K     /var/lib/docker/containers/1995f5cf52e09b3d6d2ec6760e3e29c51b645d1629a7f65415abb984a370bea4
44K     /var/lib/docker/containers/44fdc73c0e48797faffdfd4abe17ccdc3e617542d68f6f5a073bbfc343fdc0dd
44K     /var/lib/docker/containers/c3cb05fd745cfa1d527717ef63d7f9cc0dd81530d4f432538ce4fb89c0b1655f
164K    /var/lib/docker/containers/c742626988ae6237b0fc591b0604313b5ca29ebec4d91173cab3ef838d2a8967
448K    /var/lib/docker/containers/8d4ea3635d5cf9cb09b3f3652df29727288ef01adfc9963291ef53a7ed1f8a91
536K    /var/lib/docker/containers/16b599deca2f0e80024e1a342aec281c5578909c564a4a1127c92985396b91e4
1.4M    /var/lib/docker/containers/426d63b3423eaded89f4ecf1b35493f79f654903d8655af321b24307cc763e8e
5.5M    /var/lib/docker/containers/5206c2f48bd01de60a5c6576e7607ffcc6be3def17ee5dbc2be393311c2638c2
9.3M    /var/lib/docker/containers

清理单个文件

感觉哪个容器的日志太大就清理哪个

sudo sh -c "cat /dev/null > ${log_file}"
  • ${log_file} 就是日志文件

可以通过find命令查找全部日志

sudo find /var/lib/docker/containers -name *.log

/var/lib/docker/containers/a823b8ee2df7c89dcfcbc125ac835b167a4f3c4d0fc1a6ec08d359e5aefa23f0/a823b8ee2df7c89dcfcbc125ac835b167a4f3c4d0fc1a6ec08d359e5aefa23f0-json.log
/var/lib/docker/containers/99435d5cc9016af41a7f8b2761f93b4374d9774b77b1f463b429e63de7addb2a/99435d5cc9016af41a7f8b2761f93b4374d9774b77b1f463b429e63de7addb2a-json.log
/var/lib/docker/containers/8206ee47b251e97204437f6a5528b94faa2eabb385f93386f32d89efedfdb8ed/8206ee47b251e97204437f6a5528b94faa2eabb385f93386f32d89efedfdb8ed-json.log
/var/lib/docker/containers/7b3fd134954c459695092e4b79e840e7d864f6dd19b468af92ea3f83663b12b2/7b3fd134954c459695092e4b79e840e7d864f6dd19b468af92ea3f83663b12b2-json.log
/var/lib/docker/containers/dd88e4755c5bcacdbd540d21f0392c8d04a5317123a1b99ba2926ae59a72b7d0/dd88e4755c5bcacdbd540d21f0392c8d04a5317123a1b99ba2926ae59a72b7d0-json.log
/var/lib/docker/containers/7a854e0a7ca1e5029a1ac4aa689d48103d7240bded3c17f414a63368b4534da1/7a854e0a7ca1e5029a1ac4aa689d48103d7240bded3c17f414a63368b4534da1-json.log
/var/lib/docker/containers/965375d69bca2e43a4f7d3306508cc1db40fa0b783b1d7fe4b6fa7e333926b8c/965375d69bca2e43a4f7d3306508cc1db40fa0b783b1d7fe4b6fa7e333926b8c-json.log
/var/lib/docker/containers/54ef85ae58acd8fdfd028efa4e393512f4741ec3c5b0c3d690698c89c29256ac/54ef85ae58acd8fdfd028efa4e393512f4741ec3c5b0c3d690698c89c29256ac-json.log
/var/lib/docker/containers/af5b8850de8ff7d6ac3f747d263553211df3d514f5e896e624aee9a477e2db69/af5b8850de8ff7d6ac3f747d263553211df3d514f5e896e624aee9a477e2db69-json.log
/var/lib/docker/containers/16b599deca2f0e80024e1a342aec281c5578909c564a4a1127c92985396b91e4/16b599deca2f0e80024e1a342aec281c5578909c564a4a1127c92985396b91e4-json.log
/var/lib/docker/containers/ff3261b882c99365b702863ee0bda2527c21d50fee82f08583f22eec54c4534f/ff3261b882c99365b702863ee0bda2527c21d50fee82f08583f22eec54c4534f-json.log
/var/lib/docker/containers/426d63b3423eaded89f4ecf1b35493f79f654903d8655af321b24307cc763e8e/426d63b3423eaded89f4ecf1b35493f79f654903d8655af321b24307cc763e8e-json.log
/var/lib/docker/containers/90a9707a0edb5f38acff7e4bd225b4bea0e2f5aa12e56ed436af0ef6a88d1e28/90a9707a0edb5f38acff7e4bd225b4bea0e2f5aa12e56ed436af0ef6a88d1e28-json.log
/var/lib/docker/containers/c00c03b592b00931d7885aa4e9081f749ce844a9bdc2995e9d6bb46ed1277666/c00c03b592b00931d7885aa4e9081f749ce844a9bdc2995e9d6bb46ed1277666-json.log
/var/lib/docker/containers/ac6bc5a3db5be1f5c5a400d3b4d34427e55373aa50498594451bc48d73784248/ac6bc5a3db5be1f5c5a400d3b4d34427e55373aa50498594451bc48d73784248-json.log
/var/lib/docker/containers/e0136d5b180b7f85175e6a9db027a29bbcbc99ce077b96fda35987951dd5bae7/e0136d5b180b7f85175e6a9db027a29bbcbc99ce077b96fda35987951dd5bae7-json.log
/var/lib/docker/containers/8d4ea3635d5cf9cb09b3f3652df29727288ef01adfc9963291ef53a7ed1f8a91/8d4ea3635d5cf9cb09b3f3652df29727288ef01adfc9963291ef53a7ed1f8a91-json.log
...

或者查看具体容器名称的日志位置

$ docker inspect --format='{{.LogPath}}' redis
/var/lib/docker/containers/c742626988ae6237b0fc591b0604313b5ca29ebec4d91173cab3ef838d2a8967/c742626988ae6237b0fc591b0604313b5ca29ebec4d91173cab3ef838d2a8967-json.log

控制容器日志大小

以上只是临时解决的方式,最好是创建容器时就控制日志的大小.

运行时控制

启动容器时,我们可以通过参数来控制日志的文件个数和单个文件的大小

# max-size 最大数值
# max-file 最大日志数
$ docker run -it --log-opt max-size=10m --log-opt max-file=3 redis

一两个容器还好,但是如果有很多容器需要管理,这样就很不方便了,最好还是可以统一管理。

全局配置

创建或修改文件 /etc/docker/daemon.json,并增加以下配置

{
    "log-driver":"json-file",
    "log-opts":{
        "max-size" :"50m","max-file":"1"
    }
}

随后重启 Docker 服务

sudo systemctl daemon-reload
sudo systemctl restart docker

不过已存在的容器不会生效,需要重建才可以

更新于 2021-03-02
在线,41分钟前登录

查看docker更多相关的文章或提一个关于docker的问题,也可以与我们一起分享文章