清除/删除Docker日志


27

在GCE上的docker实施上,我们根文件系统上的空间不足。

由于图像本身存储在单独的1TB卷上,因此图像本身不应该成为问题。

Docker自身存储的集中式日志文件(某个地方的json文件?)是一个候选者,有人知道这些文件/文件的位置/位于何处,以及如何对其进行logrotate /截断吗?

Answers:


27

首先,我在客户端和服务器上都使用了docker 1.1.2,随着docker的快速发展,这个答案可能对于较新版本的docker来说已经过时了。

文件位置

找到您的docker目录。在使用apt / debian风格系统的系统上,docker存储库安装的软件包https://get.docker.com/ubuntu使用/var/lib/docker。该目录可能在其他系统上的同一位置(无法确认)。

在下面,containers/**CONTAINER_ID**您会找到有关该容器的信息。在该文件**CONTAINER_ID**-json.log夹中的文件中,您将找到一个包含该容器的所有日志的文件。它看起来像一个json文件,事实并非如此。这是一个json结构的流,每行一个,每行包含一个日志行(每行以a结尾,}而下一行以a 开头{,因此从整体上讲不是有效的json)。

位置示例:- /var/lib/docker/containers/05b6053c41a2130afd6fc3b158bda4e605b6053c41a2130afd6fc3b158bda4e6/05b6053c41a2130afd6fc3b158bda4e605b6053c41a2130afd6fc3b158bda4e6-json.log

编辑/更改该文件

我建议您使用该路径查看是否潮湿,这是空间不足的原因,而不是日志轮换它们。

我宁愿确保容器不会记录太多行(通过在dockerfile中使用CMD,将您的处理输出重定向到卷中的文件或/ dev / null-通过配置启用日志-并且然后,我将日志文件与另一个容器进行logrotate)


谢谢!因此,我认为我们已经确定docker仅将/ var / lib文件夹用于日志文件。就我而言,我已将其安装到单独的1 TB磁盘上,因此不会引起问题。产生大量输出的容器是mongo shard服务器,将了解如何减少其stdout的冗长性。同时,将组合一个脚本来截断这些文件,以使其在docker logs命令中有用,可能类似于logrotate,但对json-ish结构敏感。
mgmonteleone 2014年

2
docker inspect是你的朋友。
mc0e 2015年

8
docker inspect --format='{{.LogPath}}' $INSTANCE_ID
Stafford Williams

注意:对于mac用户,您的日志信息将包含在您的docker-machine
adambullmer 2016年

1
这个答案实际上并没有解决如何对docker容器日志文件进行logrotate或截断的问题。
beporter

23

这里是一个简单的一线清理容器日志文件:

echo "" > $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)


2
对于Mac用户,您必须以root用户docker-machine ssh default "echo '' | sudo tee $(docker inspect --format='{{.LogPath}}' <container_name_or_id>)"
身份

现在docker logs总是空荡荡的……
user11153 '16

整个过程无法立即生效,但docker inspect --format='{{.LogPath}}' <container_name_or_id>很有用。谢谢!
秋季伦纳德

不适用于Windows?
AP。

9

如果运行sudo lsof | grep '(deleted)',则可能会发现有许多已删除的容器日志文件被docker打开

docker    17511             root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  2879       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  6191       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  8713       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  8726       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  9907       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511  9918       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11093       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11096       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11110       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11885       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11890       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11973       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11976       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 11983       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 13105       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 16737       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17139       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17516       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17517       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17518       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17519       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17520       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17525       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17574       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17584       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17586       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17637       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17643       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17644       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17659       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17666       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17669       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17680       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17884       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 17895       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21126       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21474       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21475       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21519       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 21721       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 23829       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 23830       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 23833       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 24196       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 24197       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 26821       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 28597       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 28608       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30305       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30308       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30594       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)
docker    17511 30595       root   25r      REG              202,1      105     456624 /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)

这些是属于已删除容器的日志。关闭这些的一种方法是通过重新启动docker守护程序本身。这并不总是可行的。

释放此空间的另一种方法是找到相关的文件描述符并截断它:

ls -lh /proc/17511/fd/ | grep e49d81fc
lr-x------ 1 root root 64 Sep  8 08:06 25 -> /var/lib/docker/containers/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc/39ba30ffc0e218ee830576570222284a93ebb458eadf46933d2dd7afe49d81fc-json.log (deleted)

17511docker的pid 在哪里,是有e49d81fc问题的日志文件名/容器ID的一部分。在这里,fd是/ proc / 17511 / fd / 25,因此我们将其截断:

cat /dev/null > /proc/17511/fd/25

极好的建议!我将尝试将脚本整合在一起以实现此目的……
mgmonteleone 2014年

感谢您在重新启动docker daemon时的提示“并非总是可行”。
小鹏-ZenUML.com

为了简洁起见,您可以使用:: > /proc/17511/fd/25
mc0e '16

9

我正在使用Docker版本1.13.0,内部版本49bf474

如果您使用默认的日志记录格式json-file,则可以指定选项来轮换日志。

您可以设置docker守护程序来执行此操作,或者针对每个容器进行设置。

例如,日志文件大小为2 MB,最多5个日志文件:

Docker守护程序:创建文件/etc/docker/daemon.json

{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "2m",
        "max-file": "5",
    }
}

Docker运行:

docker run -it --log-driver json-file --log-opt max-size=2m,max-file=5 alpine ash

有关详细信息,请参阅日志记录dockerd配置


json末尾有2个逗号将导致错误
Nadir

1

答案很晚,但是同船上的任何人最好探索将其docker日志导出到易于管理的地方的选项。 logspout对于从所有Docker容器中收集日志并将其发送到某个地方非常有用。如果只需要考虑这一点,那么将它们发送到主机的syslog守护程序可能是一个好方法,并且您可以使用通常的机制将它们排列到文件中并从那里旋转它们。对于较大的多主机环境,您可能会考虑将其运送到“ ELK”堆栈。即Elasticsearch,Logstash和Kibana。


0

要找到码头工人的位置,您可以使用:

docker info
Containers: 16
Images: 45
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Dirs: 78
Execution Driver: native-0.2
Kernel Version: 3.16.0-31-generic
Operating System: Ubuntu 14.10
WARNING: No swap limit support

如果要释放空间,可以采用以下方式:

docker ps -a
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                     PORTS               NAMES
2e2977ed2f4c        ekino/puppetlabs:12.04.5   "/bin/bash"         15 hours ago        Exited (0) 15 hours ago                        stoic_jones             
ded5903df161        ekino/puppetlabs:12.04.5   "/bin/bash"         12 days ago         Exited (0) 11 days ago                         boring_wilson           
72a831b7e4ab        a964750d8f91               "/bin/bash"         4 weeks ago         Exited (127) 3 weeks ago                       hungry_meitner          
79af8331bad9        a964750d8f91               "/bin/bash"         4 weeks ago         Exited (0) 4 weeks ago                         tender_wilson           
d3d3ca34b4b2        1af3728b2912               "/bin/bash"         6 weeks ago         Exited (0) 5 weeks ago                         distracted_shockley     

现在,您可以使用以下方法删除容器:

docker rm ac055c58ab19

0

在官方Docker文档中看到了该脚本:

sudo find /var/lib/docker/containers/ -type f -name "*.log" -delete

工作正常,但是docker不会自动释放内存...该脚本只是删除日志文件。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.