将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
将附加到您引用的服务(容器)上,并且
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屋!