簡體   English   中英

docker日志和緩沖輸出

[英]docker logs and buffered output

我想連續打印沒有換行的點(等待行為)。

這個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

我可以通過在進程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

接下來我嘗試將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日志記錄驅動程序(json-file)不支持無緩沖輸出。 你最終會看到你的點,但只有在填充緩沖區之后。

如果需要無緩沖輸出,可以使用syslog日志記錄驅動程序。 但是,因為它是系統日志,我懷疑它會給你你想要的東西,因為它將向syslog提供無緩沖的輸出。 在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM