docker日志和缓冲的输出 [英] docker logs and buffered output

查看:81
本文介绍了docker日志和缓冲的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连续打印点而不换行(等待行为).

I want to continously print dots without newline (waiting behavior).

这种bash单线在我的机器上可以正常工作:

This bash one-liner works fine on my machine:

$ while true; do sleep 1; printf '.'; done
.......^C

但是,当我在Docker容器中运行它时,当我尝试使用docker日志读取其输出时,没有打印输出:

However, when I run it in a Docker container, and when I try to read its output with docker logs, no output is printed:

$ docker run -d --name test_logs ubuntu:14.04 bash -c "while true; do sleep 1; printf '.'; done"
60627015ed0a0d331a26e0c48ccad31c641f2142da55d24e10f7ad5737211a18
$ docker logs test_logs
$ docker logs -f test_logs
^C

我可以通过在进程1(bash命令)上使用 strace 来确认bash循环正在容器中执行:

I can confirm that the bash loop is executing in the container by using strace on process 1 (bash command):

$ docker exec -t test bash -c 'apt-get install -y strace; strace -p1 -s9999 -e write'
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  strace
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 113 kB of archives.
After this operation, 504 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main strace amd64 4.8-1ubuntu5 [113 kB]
Fetched 113 kB in 0s (154 kB/s)
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
Selecting previously unselected package strace.
(Reading database ... 11542 files and directories currently installed.)
Preparing to unpack .../strace_4.8-1ubuntu5_amd64.deb ...
Unpacking strace (4.8-1ubuntu5) ...
Setting up strace (4.8-1ubuntu5) ...
Process 1 attached
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=136, si_status=0, si_utime=0, si_stime=0} ---
write(1, ".", 1)                        = 1
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=153, si_status=0, si_utime=0, si_stime=0} ---
write(1, ".", 1)                        = 1
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=154, si_status=0, si_utime=0, si_stime=0} ---
write(1, ".", 1)                        = 1
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=155, si_status=0, si_utime=0, si_stime=0} ---
write(1, ".", 1)                        = 1

...等等.

此外,当直接使用选项 -t (不使用 docker日志)监视输出时,它也可以正常工作:

Also, it works fine when directly watching output with option -t (without using docker logs):

$ docker run -t --name test_logs ubuntu:14.04 bash -c "while true; do sleep 1; printf '.'; done"
...........^C

即使是怪异的,背景+伪tty(选项 -d +选项 -t )也曾经工作过,但后来就不再工作了.

Even weirder, background + pseudo-tty (option -d + option -t) worked once, but then didn't work anymore.

printf 是行缓冲命令,如果我通过打印换行符来添加刷新,则它会起作用:

printf is a line-buffered command, and if I add flushing by printing a newline character, it works:

$ docker run -d --name test_logs ubuntu:14.04 bash -c "while true; do sleep 1; printf '.\n'; done"
720e274fcf85f52587b8a2a402465407c5e925c41d80af05ad3a73cebaf7110f
$ docker logs -f test_logs
.
.
.
.
.
.
^C

所以我尝试用stdbuf取消缓冲" printf ,但没有成功:

So I tried to "unbuffer" printf with stdbuf, without any success:

$ docker run -d --name test_logs ubuntu:14.04 bash -c "while true; do sleep 1; stdbuf -o0 printf '.'; done"
2ba2116190c1b510288144dc5a220669f52f701c17f6f102e6bd6af88de4674e
$  docker logs test_logs
$ docker logs -f test_logs
^C

接下来,我尝试将 printf 重定向到标准错误,但仍然没有成功:

Next I tried to redirect printf to the standard error, still without success:

$ docker run -d --name test_logs ubuntu:14.04 bash -c "while true; do sleep 1; printf '.' >&2; done"
b1645b48bd9afd5b72318fba5296157ce1c0346f6a82fa166e802a979c1b0b0f
$ docker logs test_logs
$ docker logs -f test_logs
^C

...同时进行,但仍然没有成功:

...and with both at the same time, still without success:

$  docker run -d --name test_logs ubuntu:14.04 bash -c "while true; do sleep 1; stdbuf -o0 printf '.' >&2; done"

使用 echo -n 而不是 printf 时,我遇到了相同的行为.

I encountered the same behaviour when using echo -n instead of printf.

我的问题:我是否可以正确缓冲?如果是,什么使它不起作用?

My questions: Do I unbuffer correctly? If yes, what makes it not working?

寻找对此的见解:)

推荐答案

默认的docker日志记录驱动程序(json文件)不支持无缓冲输出.最终,只有在缓冲区被填满之后,您才会看到点.

The default docker logging driver (json-file) does not supported unbuffered output. You will eventually see your dots, but only after the buffer has been filled.

如果需要无缓冲输出,则可以使用syslog日志记录驱动程序.但是,因为它是syslog,所以我怀疑它会为您提供所需的内容,因为它将向syslog提供无缓冲的输出.在docs.docker上记录的此处.com

If you need unbuffered output, you can use the syslog logging driver. However, because it's syslog, I doubt that will give you what you want since it will be delivering unbuffered output to syslog. Documented here on docs.docker.com

由于运行点通常仅在交互式情况下才有用,在交互式情况下,看到更多的点意味着工作仍在继续,因此您可能只想使用换行符来显示定期状态,以便docker日志为您提供更好的信息.

Since running dots are usually useful only in interactive situations where seeing more dots means that work is continuing, you may just want to put out a periodic status with a newline so docker logs gives you better information.

这里的解决方案通常是检查您是否处于交互模式,如果是,则显示点:

The solution here is usually to check to see if you are in interactive mode, and if so, show the dots:

#!/bin/bash
while true; do
     if [ -t ]; then
         echo -n .
     elif ((++COUNT % 60 == 0)); then
         echo "$(date) still in progress"
     fi
     sleep 1   # or whatever work is going on
done

这篇关于docker日志和缓冲的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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