2021年1月22日 星期五

[ Docker 常見問題 ] How to clear the logs properly for a Docker container?

 Source From Here

Question
I use docker logs [container-name] to see the logs of a specific container. Is there an elegant way to clear these logs?

HowTo
First the bad answer. From this question there's a one-liner that you can run:
$ echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

instead of echo, there's the simpler:
$ : > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

or there's the truncate command:
$ truncate -s 0 $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)


I'm not a big fan of either of those since they modify Docker's files directly. The external log deletion could happen while docker is writing json formatted data to the file, resulting in a partial line, and breaking the ability to read any logs from the docker logs cli. For an example of that happening, see this comment on duketwo's answer

Instead, you can have Docker automatically rotate the logs for you. This is done with additional flags to dockerd if you are using the default JSON logging driver:
$ dockerd ... --log-opt max-size=10m --log-opt max-file=3

You can also set this as part of your daemon.json file instead of modifying your startup scripts:
  1. {  
  2.   "log-driver""json-file",  
  3.   "log-opts": {"max-size""10m""max-file""3"}  
  4. }  
These options need to be configured with root access. Make sure to run a systemctl reload docker after changing this file to have the settings applied. This setting will then be the default for any newly created containers. Note, existing containers need to be deleted and recreated to receive the new log limits.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...