简体   繁体   English

Docker - 如何查看 Docker 容器内运行应用程序的日志?

[英]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.

相关问题 在另一个Docker容器中运行一个Docker容器? - Running a docker container inside another docker container? 如何在Docker容器中监听运行NestJS的NodeJS的不同通道的日志? - How to listen to different channels of logs of NodeJS running NestJS in a Docker container? 如何在docker容器中运行的nodejs应用程序内使用child_process - How to use child_process inside nodejs application running inside docker container 从另一个docker容器内部访问在docker容器中运行的服务 - Access service running in docker container from inside another docker container 如何调试运行在docker容器内的节点程序 - how to debug node program running inside docker container 如何为在 docker 容器内运行的快速服务器启用 xvfb? - How to enable xvfb for an express server running inside a docker container? Docker - 如何等待容器运行 - Docker - How to wait for container running "如何通过用户名和密码保护在 Docker 容器中运行的 Web 应用程序?" - How to protect a web application running in a Docker container by username and password? 在哪里可以看到使用Docker映像和容器运行的应用程序代码 - Where can I see the code of application that is running using docker images and container 如何安装分离到Git子模块的NPM应用程序(在Docker容器内部) - How to install NPM application (inside Docker container) that was separated to Git Submodule
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM