将在Docker容器中创建的日志文件重定向到stdout / stderr [英] redirect log files created in a docker container to stdout / stderr

查看:1059
本文介绍了将在Docker容器中创建的日志文件重定向到stdout / stderr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Dockerfile:

I have the following Dockerfile:

FROM ubuntu:16.04
RUN apt-get update
VOLUME ["/LOGS"]
COPY ./testServer .
ENTRYPOINT ./testServer 8600

testServer具有正在写入的日志文件。它们位于目录 LOGS中。每次启动 testServer时,都会创建一个新日志。我想做的是将目录中的最新日志文件尾巴到stdout / stderr。

"testServer" has log files that are being written to. They are located in the directory "LOGS". Each time "testServer" is started, a new log is created. What I wanted to do was to "tail" the latest log file in the directory to stdout / stderr.

我尝试添加:

CMD ["/bin/sh", "-c", "tail $( ls -Art /LOGS | tail -n 1 ) > out_server.log 2>&1"]

到Dockerfile(并在之后重新构建映像),但是它不起作用。

to the Dockerfile (and rebuilt the image thereafter) but it did not work.

这怎么办?

TIA

推荐答案

代替使用tail,可以将日志文件符号链接到容器进程的stdout。为此,您需要将可执行文件包装在单独的脚本中,以便将其作为与容器主进程分开的进程启动。

Instead you using tail, you can symlink the log file to the container process's stdout. To do so, you need to wrap your executable in a separate script so it gets launched as a process separate from the container's main process.

要执行的脚本:

#!/bin/bash

# The application log will be redirected to the main docker container process's stdout, so # that it will show up in the container logs
touch /my/app/application.log
ln -sf /proc/1/fd/1 /my/app/application.log

# Execute my app
./testServer 8600

然后在docker文件中复制并执行脚本

And in the docker file just copy and execute the script

COPY start_server.sh /the/above/script/start_server.sh
CMD ["/bin/bash", "/the/above/script/start_server.sh"]

这篇关于将在Docker容器中创建的日志文件重定向到stdout / stderr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆