[英]Docker - How to see the logs of running application inside Docker container?
I have a Node.js application running inside Docker container in /usr/src/app.我在 /usr/src/app 的 Docker 容器中运行了一个 Node.js 应用程序。
I want to run this application using nohup (nohup node index.js &).我想使用 nohup (nohup node index.js &) 运行这个应用程序。
What is the best way to see the output appended to nohup.out for general/debugging purposes?出于一般/调试目的,查看附加到 nohup.out 的输出的最佳方法是什么?
Do I need to use docker cp all the time to copy to host os.我是否需要一直使用 docker cp 来复制到主机操作系统。 Is there a better way to achieve this?有没有更好的方法来实现这一目标?
Thanks.谢谢。
Although using logfiles inside container (without volume mounting) is an anti-pattern, you can easily output those files using exec for example along with cat:尽管在容器内使用日志文件(没有卷挂载)是一种反模式,但您可以使用 exec 和 cat 轻松输出这些文件:
docker exec -it [CONTAINERID] cat /usr/src/app/nohup.out
Better pattern would be to store output into separate volume-mounted folder removing state from inside your container, but would also allow you to access logs directly from host system.更好的模式是将输出存储到单独的卷安装文件夹中,从容器内部删除状态,但也允许您直接从主机系统访问日志。
In this specific case, I don't see a need for a separate logfile, and not even for nohup.在这种特定情况下,我认为不需要单独的日志文件,甚至不需要 nohup。 Just set policy for your container to restart it automatically ( --restart always
) instead of nohup.只需为您的容器设置策略以自动重新启动它( --restart always
)而不是 nohup。
You do output your logs in你确实输出了你的日志
/dev/stdout
for all normal logs /dev/stdout
用于所有正常日志
use /dev/stderr
to pipe all the errors you have, if your application can differ between normal logs and error logs.如果您的应用程序在正常日志和错误日志之间可能有所不同,请使用/dev/stderr
来传递您遇到的所有错误。
So instead of defining your log like /var/log/nohup.log
you define the logfile to be dev/stdout
因此,不要像/var/log/nohup.log
那样定义日志,而是将日志文件定义为dev/stdout
Accessing the logs will then be as easy as writing docker logs <containername>
or docker-compose logs <servicename>
- if you have started your stack using docker-compose up
you will see the logs right in front of you anyway - very convenient.访问日志就像编写docker logs <containername>
或docker-compose logs <servicename>
一样简单——如果你已经使用docker-compose up
启动了你的堆栈,无论如何你都会在你面前看到日志——非常方便。
In production grade you want to process the log stream further, look at https://github.com/gliderlabs/logspout在生产级,您想进一步处理日志流,请查看https://github.com/gliderlabs/logspout
And if you want to go a level deeper, add the ELK stack to it https://logz.io/learn/complete-guide-elk-stack/如果您想更深入,请将 ELK 堆栈添加到其中https://logz.io/learn/complete-guide-elk-stack/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.