【问题标题】:docker logs and buffered outputdocker 日志和缓冲输出
【发布时间】:2016-07-13 01:49:05
【问题描述】:

我想在没有换行符的情况下连续打印点(等待行为)。

这个 bash 单行在我的机器上运行良好:

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

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

$ 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 上使用 strace(bash 命令)来确认 bash 循环正在容器中执行:

$ 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 logs)直接观看输出时,它也可以正常工作:

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

更奇怪的是,背景 + 伪 tty(选项 -d + 选项 -t)工作了一次,但后来就不再工作了。

printf 是一个行缓冲命令,如果我通过打印换行符来添加刷新,它可以工作:

$ 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,但没有任何成功:

$ 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重定向到标准错误,仍然没有成功:

$ 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

...同时使用两者,仍然没有成功:

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

我在使用 echo -n 而不是 printf 时遇到了同样的行为。

我的问题:我是否正确取消缓冲?如果是,是什么导致它不起作用?

寻找这方面的见解:)

【问题讨论】:

  • 请记住,您可以执行docker run -ddocker run -it 但不能执行docker run -itd 您想作为守护进程运行,还是与终端关联?
  • @user2915097 好的,我明白了。我想将它作为守护进程运行,但我正在尝试一些选项组合来分析问题

标签: bash logging docker buffer bufferedoutputstream


【解决方案1】:

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

如果您需要无缓冲的输出,您可以使用 syslog 日志记录驱动程序。但是,因为它是 syslog,所以我怀疑它会给您想要的东西,因为它将向 syslog 提供无缓冲的输出。记录在案的here on docs.docker.com

由于运行点通常仅在看到更多点意味着工作仍在继续的交互情况下才有用,您可能只想用换行符输出周期性状态,以便 docker logs 为您提供更好的信息。

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

#!/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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2017-01-22
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多