2020年9月3日 星期四

[ 常見問題 ] How to clean Docker container logs?

 Source From Here

Question
I read my Docker container log output using "docker logs":
# docker logs -f <container_name>

I log lots of data to the log in my node.js app via calls to console.log()I need to clean the log, because it's gotten too long and the docker logs command first runs through the existing lines of the log before getting to the end. How do I clean it to make it short again?

How-To
First, if you just need to see less output, you can have docker only show you the more recent lines:
// --since: Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
# docker logs --since 30s -f <container_name_or_id>

Or you can put a number of lines to limit:
// --tail: Number of lines to show from the end of the logs (default:all)
# docker logs --tail 20 -f <container_name_or_id>

To delete the logs on a Docker for Linux install, you can run the following for a single container:
# echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)

Note that this requires root, and I do not recommend this. You could potentially corrupt the logfile if you null the file in the middle of docker writing a log to the same file. Instead you should configure docker to rotate the logs.

Lastly, you can configure docker to automatically rotate logs with the following in an /etc/docker/daemon.json file:
  1. {  
  2.   "log-driver""json-file",  
  3.   "log-opts": {"max-size""10m""max-file""3"}  
  4. }  
That allows docker to keep up to 3 log files per container, with each file limited to 10 megs (so a limit between 20 and 30 megs of logs per container). You will need to run a systemctl reload docker to apply those changes. And these changes are the defaults for any newly created container, they do not apply to already created containers. You will need to remove and recreate any existing containers to have these settings apply.

沒有留言:

張貼留言

[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...