欢迎来到好人卡资源网,专注网络技术资源收集,我们不仅是网络资源的搬运工,也生产原创资源。寻找资源请留言

一键清理docker日志文件脚本

linux sky995 2年前 (2020-11-28) 462次浏览 0个评论

docker运行时间长了,会有大量的日志

如果不定时清理,可能会导致磁盘空间不足

在linux上,容器日志一般存放在/var/lib/docker/containers/container_id/下面, 以json.log结尾的文件(业务日志)很大,查看各个日志文件大小的脚本docker_log_size.sh,内容如下

<code><span class="lit">1</span> <span class="com">#!/bin/sh </span>
<span class="lit">2</span><span class="pln"> echo </span><span class="str">"======== start clean docker containers logs ========"</span>  
<span class="lit">3</span><span class="pln"> logs</span><span class="pun">=</span><span class="pln">$</span><span class="pun">(</span><span class="pln">find </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="pln">docker</span><span class="pun">/</span><span class="pln">containers</span><span class="pun">/</span> <span class="pun">-</span><span class="pln">name </span><span class="pun">*-</span><span class="pln">json</span><span class="pun">.</span><span class="pln">log</span><span class="pun">)</span>  
<span class="lit">4</span> <span class="kwd">for</span><span class="pln"> log </span><span class="kwd">in</span><span class="pln"> $logs  
</span><span class="lit">5</span>         <span class="kwd">do</span>  
<span class="lit">6</span><span class="pln">                 echo </span><span class="str">"clean logs : $log"</span>  
<span class="lit">7</span><span class="pln">                 cat </span><span class="pun">/</span><span class="pln">dev</span><span class="pun">/</span><span class="kwd">null</span> <span class="pun">></span><span class="pln"> $log  
</span><span class="lit">8</span>         <span class="kwd">done</span>  
<span class="lit">9</span><span class="pln"> echo </span><span class="str">"======== end clean docker containers logs ========"</span>  </code>

当然,如果要根治的话,在启动的时候就要限制日志log的大小


好人卡资源网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一键清理docker日志文件脚本
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址