stdout being buffered in docker container

后端 未结 2 1942
刺人心
刺人心 2021-02-09 11:45

I\'m not entirely sure what is going on here but it appears that stdout is being buffered when I run my code in a container, but not if I run it on the host or on OSX.

h

相关标签:
2条回答
  • 2021-02-09 12:07

    Is this what you're asking?

    Why am I seeing my output in chunks? Is it being buffered and flushed somewhere?

    Yes. Your Docker container is running on a Docker engine (possibly wrapped by a Docker machine) and your commands are being run from a client. Even if your engine is installed locally and you're running your container in interactive mode, the commands you're entering and the output you're seeing are still basically the results of HTTP requests against localhost. (Or Unix Socket or TTY communications.) (I'm simplifying.) Docker is routing I/O through your network drivers, i.e. its stdout has to be flushed to a (virtual?) network connection, and then the client flushes that to your stdout.

    In background mode, the engine captures stdout and (by default) writes to a file. The docker logs command sends a request from your client to the engine, and the engine responds by reading back the contents of that file.

    0 讨论(0)
  • 2021-02-09 12:13

    I faced same issue and looks like root cause is "stdout buffering". You can start reading http://www.pixelbeat.org/programming/stdio_buffering/ and google in case of any additional question. I failed to resolve it by "stdbuf" or any linux/app settings and changed source code of the console app I want to dockerize to proceed with file flush on each write.

    (my configuration is strange because of a lot of reason, but it is: the app has configured log output to the file named "/dev/stdout" and it performs file_flush on each log write and docker can show messages in real time on "docker logs -f XXXXX")

    0 讨论(0)
提交回复
热议问题