docker日志和缓冲输出

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

这个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(bash命令)上使用strace来确认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“unbuffer” 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 

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

 $ 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时,我遇到了同样的问题。

我的问题:我的缓冲区是否正确? 如果是,是什么让它不能工作?

寻找这方面的见解:)