docker日志一般存储在/var/lib/docker/containers/<container_id>
下,文件名为<container_id>-json.log
,docker中的日志全部由这个文件负责记录,如果业务量大的话,这个文件大小增长会非常快,若在其他地方记录业务日志的话,那么这个docker日志可以控制它的体积。
nginx:
image: nginx:1.12.1
restart: always
logging:
driver: "json-file"
options:
max-size: "5g"
docker-compose.yml官方介绍里是这么说的,限制日志大小为5GB。不过当我实验的时候发现并不认logging这个参数,后来发现是yml文件中version的版本写的过低,这里推荐写3
即可,如:
version: '3'
services:
redis:
image: redis:5.0
restart: always
container_name: IP20-redis-6378
environment:
- TZ=Asia/Shanghai
ports:
- 6378:6378
volumes:
- ./redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
logging:
driver: "json-file"
options:
max-size: "5g"
补充:若想关闭docker中的日志,使之不生成log文件,则配置如下:
version: '3'
services:
redis:
image: redis:5.0
restart: always
container_name: IP20-redis-6378
environment:
- TZ=Asia/Shanghai
ports:
- 6378:6378
volumes:
- ./redis.conf:/etc/redis/redis.conf
command: redis-server /etc/redis/redis.conf
logging:
driver: "none"
或者在docker run
命令中加上-log-driver=none
参数即可
在/etc/docker/daemon.json
文件中添加关于日志的参数(无此文件的可以新建):
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"log-driver": "json-file",
"log-opts": {
"max-size": "1m",
"max-file": "1"
}
}
因为我在其他地方记录了业务日志,所以docker中的日志就尽量设置小一点,我设置了最大文件大小为1MB,文件数量为1个。
设置之后重启docker服务:
systemctl daemon-reload
systemctl restart docker
注意:设置的日志大小,只对新建的容器有效
看下实际效果:
[root@r-d-47 60084fbb27418505fc91c70b5ac866d96b6a3d8683dfd18ae161e9ee293e3451]# ls | xargs du -sh
960K 60084fbb27418505fc91c70b5ac866d96b6a3d8683dfd18ae161e9ee293e3451-json.log
0 checkpoints
8.0K config.v2.json
4.0K hostconfig.json
4.0K hostname
4.0K hosts
0 mounts
4.0K resolv.conf
4.0K resolv.conf.hash
参考资料:https://blog.csdn.net/yjk13703623757/article/details/80283729
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:彻底解决Docker日志过大问题