11

#docker 设置日志文件限制防止磁盘被占满

 3 years ago
source link: https://xmanyou.com/docker-logging-file-limitation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
2 March 2021 / docker

#docker 设置日志文件限制防止磁盘被占满

使用docker时,如果不设置日志文件的大小,所有的docker容器输出的日志都会保存下来,直到占满所有的磁盘空间。

这是因为,默认的日志设置为json-file,为了与旧版本docker兼容,这个设置没有添加日志限制。

来自官方的提示:

https://docs.docker.com/config/containers/logging/configure/

docker-file-limitation-01

1. 修改全局日志选项

  • 1). 打开docker的daemon.json文件
sudo vi /etc/docker/daemon.json
  • 2). 添加日志限制
    可以使用默认有日志限制的local日志
{
...
  "log-driver": "local"
}

关于local日志的默认值

https://docs.docker.com/config/containers/logging/local/

  • max-size=20m
  • max-file=5
  • compress=false

或者
为默认的json-file日志选项添加限制

https://docs.docker.com/config/containers/logging/json-file/

{
    ...
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  }
}
  • 3). 重启docker
sudo systemctl restart docker
  • 4). 重新创建容器
    docker重启后,所有新容器都会使用新的日志选项,但是已经创建容器则会继续使用旧的选项。
    所以,如果旧的容器还在运行,需要重新创建。

2. 为每个容器单独设置日志选项

docker run -it --log-opt mode=non-blocking --log-opt max-buffer-size=4m alpine ping 127.0.0.1

更多的日志选项,可以参考官方文档。

https://docs.docker.com/config/containers/logging/configure/

docker-file-limitation-02

阿斌

Read more posts by this author.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK