如何将Docker容器日志重定向到单个文件? [英] How to redirect docker container logs to a single file?

查看:516
本文介绍了如何将Docker容器日志重定向到单个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将docker容器的所有日志重定向到单个日志文件以进行分析.我尝试过

I want to redirect all the logs of my docker container to single log file to analyse them. I tried

docker logs container > /tmp/stdout.log 2>/tmp/stderr.log

但是这使登录两个不同的文件.我已经尝试过

but this gives log in two different file. I already tried

docker logs container > /tmp/stdout.log

但是没有用.

推荐答案

无需重定向日志.

默认情况下,Docker将日志存储到一个日志文件.要检查日志文件路径,请运行命令:

Docker by default store logs to one log file. To check log file path run command:

docker inspect --format='{{.LogPath}}' containername

/var/lib/docker/containers/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334-json.log

打开该日志文件并进行分析.

Open that log file and analyse.

如果您重定向日志,则只能在重定向之前获取日志.您将看不到实时日志.

if you redirect logs then you will only get logs before redirection. you will not be able to see live logs.

要查看实时日志,可以在以下命令下运行

To see live logs you can run below command

tail -f `docker inspect --format='{{.LogPath}}' containername`

注意:

仅当docker生成日志时(如果没有日志,则此文件将不存在),才会创建此日志文件/var/lib/docker/containers/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334-json.log.如果我们运行命令docker logs containername,它类似于某个时间,但它什么也不返回.在这种情况下,该文件将不可用.

This log file /var/lib/docker/containers/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334/f844a7b45ca5a9589ffaa1a5bd8dea0f4e79f0e2ff639c1d010d96afb4b53334-json.log will be created only if docker generating logs if there is no logs then this file will not be there. it is similar like sometime if we run command docker logs containername and it returns nothing. In that scenario this file will not be available.

这篇关于如何将Docker容器日志重定向到单个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆