简体   繁体   English

docker 上的 Python 打印到标准输出

[英]Python on docker to print out to stdout

I'm running a docker container that executes commands with server.我正在运行一个使用服务器执行命令的 docker 容器。 It then logs the output to files.然后它将 output 记录到文件中。 I have another docker that runs every few minutes and pick up everything from docker log {name} .我有另一个 docker 每隔几分钟运行一次,并从docker log {name}中获取所有内容。 I'm looking for a way to read the executions log files and print it to STDOUT for the logger service to pick the data to.我正在寻找一种方法来读取执行日志文件并将其打印到STDOUT以供记录器服务选择数据。 I tried something like: subprocess.call("cat {latest_file}".format(latest_file=latest_file), shell=True) but it only print it to console whilst running.我尝试了类似: subprocess.call("cat {latest_file}".format(latest_file=latest_file), shell=True)但它只在运行时将其打印到控制台。

my question is: can I add my own files/directories to the docker logger?我的问题是:我可以将自己的文件/目录添加到 docker 记录器吗?

Assuming that you know the name of the log file beforehand, you can let the application continue to log as is (ie to a file) and symlink that file to /dev/stdout or /dev/stderr假设您事先知道日志文件的名称,您可以让应用程序继续按原样记录(即到文件)并将该文件符号链接到/dev/stdout/dev/stderr

This is a quite common solution, for example nginx does it这是一个很常见的解决方案,例如nginx就可以了

# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

EDIT: Please note that this will only work if the applications that do logging is running as PID 0. If you have forked a process or similar, you will have to explicitly write to the stdout/stderr file descriptor of PID 1 (available under /proc/1/fd编辑:请注意,这仅在执行日志记录的应用程序以 PID 0 运行时才有效。如果您已经分叉了一个进程或类似的进程,则必须显式写入 PID 1 的 stdout/stderr 文件描述符(可在/proc/1/fd

# forward request and error logs to docker log collector
RUN ln -sf /proc/1/fd/1 /var/log/nginx/access.log \
    && ln -sf /proc/1/fd/2 /var/log/nginx/error.log

(Please see this answer for more details) (有关详细信息,请参阅此答案

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

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