简体   繁体   English

如何将 docker 容器的 STDOUT 捕获到该容器外部的文件中?

[英]How can I capture STDOUT of a docker container to a file OUTSIDE of that container?

I have a docker container that when I run it, it automatically stops (because of the application crashing) and I cannot figure out why it is crashing because I have found no way to capture the STDOUT of the docker application to a file on the host file system.我有一个 docker 容器,当我运行它时,它会自动停止(因为应用程序崩溃)并且我无法弄清楚为什么它会崩溃,因为我找不到将 docker 应用程序的 STDOUT 捕获到主机上的文件的方法文件系统。 Without this, when my app crashes, my docker container shuts down and is removed automatically, so there is no container to attach to or get logs from.如果没有这个,当我的应用程序崩溃时,我的 docker 容器会关闭并自动删除,因此没有容器可以附加或从中获取日志。

I have found no help in the docker documentation for how to solve this issue.我在 docker 文档中找不到有关如何解决此问题的帮助。

There is a multitude of possibilities for this, some of them mentioned in the comments.这有多种可能性,其中一些在评论中提到。

For me, doing redirects or even mounting volumes for the sake of logging is somewhat out of the docker scope. Instead, using a logdriver and checking the logs on the host or central log system seems to be the right approach.对我来说,为了日志记录而进行重定向甚至安装卷有点超出了 docker scope。相反,使用日志驱动程序并检查主机或中央日志系统上的日志似乎是正确的方法。

This can be done in a pretty easy way.这可以通过非常简单的方式完成。 Let's assume you want to run a simple nginx container.假设您要运行一个简单的 nginx 容器。

docker run -dt --name web01 -p 8080:80 docker.io/library/nginx:latest

This will do the logging as stated from your post (STDOUT of the container), which can be checked via docker logs .这将按照您的帖子(容器的 STDOUT)中所述进行日志记录,可以通过docker logs进行检查。

If you want to have the log somewhere else, you run something like:如果你想把日志放在别处,你可以运行如下命令:

docker run -dt --name web01 --log-driver journald -p 8080:80 docker.io/library/nginx:latest

Which will produce log entries in journald.这将在 journald 中生成日志条目。

I am not entirely sure how you want to get your hands on the logs, but all options are documented, very well.我不完全确定你想如何获得日志,但所有选项都记录在案,非常好。

https://docs.docker.com/config/containers/logging/configure/https://docs.docker.com/config/containers/logging/configure/

You can even change the default logdriver (for all containers), if this is your desired behaviour.如果这是您想要的行为,您甚至可以更改默认日志驱动程序(适用于所有容器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM