1. 为什么要清理?
在容器运行的过程中,通常会产生大量的日志,尤其是应用程序本身记录了info级别的日志时候,程序的标准输出记录到容器的日志。这样会占用大量的磁盘空间,严重者导致IO异常,最终服务会宕机。
2. 日志说明
Docker 容器日志存储在 /var/lib/docker/,文件格式是json格式,并通过容器ID进行区分。存储路径如下:/var/lib/docker/containers/ID/ID-json.log
[root@Reseach conf]# ll /var/lib/docker/containers
total 76
drwx-----x 4 root root 4096 Feb 23 09:45 10d8b6d6ee8a756061052e1c027a96d1a5b30c1
drwx-----x 4 root root 4096 Feb 23 09:45 2aa280b3c0c314929a71a83436b87cccc1ad516
drwx-----x 4 root root 4096 Mar 17 17:47 34031998d9b6f3df34b0ab466e38a84cbad1e12
[root@Reseach 10d8b6d6ee8a756061052e1c027a96d1a5b30c14d3a88856e3180b7e14adeb5d]# tree -L 2 ./
./
├── 10d8b6d6ee8a756061052e1c027a96d1a5b30c14d3a88856e3180b7e14adeb5d-json.log
├── checkpoints
├── config.v2.json
├── hostconfig.json
├── hostname
├── hosts
├── mounts
├── resolv.conf
└── resolv.conf.hash
3. 日志查看方式
3.1 docker log
docker logs -f 容器ID
3.2 docker-compose logs
滚动查看最后20行,并现实时间戳。docker-compose logs -f -t --tail=20
4. 清理
找到对应的日志文件:docker inspect --format='{{.LogPath}}' CONTAINER_ID
进行清理:cat /dev/null > xxx-json.log
5. 防患于未然
针对日志打印频繁的服务,可以直接在docker-compose.yml中配置日志记录选项,这样有助于Docker自己清理日志。
version: "3"
services:
ShardingSphereProxy:
image: apache/shardingsphere-proxy
container_name: shardingsphere-proxy
network_mode: "bridge"
restart: always
command: server /data
ports:
- 13307:3307
volumes:
- ./conf:/opt/shardingsphere-proxy/conf
- ./ext-lib:/opt/shardingsphere-proxy/ext-lib
environment:
- JVM_OPTS="-Djava.awt.headless=true"
# 日志记录文件数量和文件大小
logging:
driver: "json-file"
options:
max-size: "200m"
max-file: "3"
转自:https://blog.csdn.net/oschina_41731918/article/details/124618317