将 docker-compose 日志保存到文件中 [英] Save docker-compose logs to a file
问题描述
我在构建服务器上运行了单元测试,并希望在出现故障时捕获日志结果以进行分析.我还没有找到一种方法将 docker-compose logs
的输出重定向到一个文件,或者找到日志文件本身的实际位置.
I have unit tests running on my build server and would like to capture the log results for analysis when something fails. I have yet to find a way to redirect the output of docker-compose logs
to a file, or to find where the log files themselves actually live.
我想要相当于:
docker-compose logs > logs.txt
<小时>
编辑 - 说明:
Edit - clarification:
我所有的 docker 容器都会生成有用的日志,手动运行 docker-compose logs
会显示这些日志.我想编写此过程的脚本以将这些相同的日志保存到我的构建服务器上的工件文件中.本质上,docker-compose logs
的输出保存到文件中,但是 docker-compose logs
永远不会退出.
All of my docker containers produce useful logs, which a manual run of docker-compose logs
reveals. I want to script this process to save those same logs to a file that is an artifact on my build server. Essentially, the output of docker-compose logs
saved to a file, however docker-compose logs
never exits.
推荐答案
默认情况下,docker 使用 json-file
驱动程序来记录您的容器日志,日志的原始 json 输出可以在:
By default docker uses the json-file
driver to record your containers logs and the raw json output of the logs can be found in:
/var/lib/docker/containers/[container-id]/[container-id]-json.log
您可以通过运行获取此位置:
You can get this location by running:
docker inspect --format='{{.LogPath}}' [container-id or container-name]
当您运行 docker-compose logs [service-name]
时,docker-compose
将附加到您引用的服务(容器)和 LogPrinter 对象将输出上述文件的内容,但格式如此它们更容易阅读.
When you run docker-compose logs [service-name]
, docker-compose
will attach to the service (container) you reference and the LogPrinter object will output the contents of the above file, but formatted so they're easier to read.
相关文档:https://docs.docker.com/compose/compose-文件/#logging
这篇关于将 docker-compose 日志保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!