docker日志和缓冲输出?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (512)

我想连续打印没有换行的点(等待行为)。

这个bash one-liner在我的机器上工作正常:

$ 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

我可以通过使用strace进程1(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

所以我尝试printf用stdbuf “unbuffer” ,没有任何成功:

$ 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

我的问题:我是否正确地解压缩?如果是的话,是什么让它不起作用?

提问于
用户回答回答于

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

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

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励