Given the following simple example (app.rb) and nothing more
# -*- coding: utf-8 -*-
require 'sinatra'
set :bind, '0.0.0.0'
puts "WOOT a puts statement was HERE"
get '/' do
"hello world"
end
Executing this via
bundle exec ruby app.rb
Renders both the puts statement and the expected sinatra behavior.
However when the same code is executed within a Docker container with the same execution command only the Sinatra output is visible. The puts statement never gets rendered.
However if you comment everything but the 'puts' statement and execute it in docker the puts statement is rendered correctly. The difference in docker behavior seems to be dependent on whether sinatra is required or not.
I seem to be missing something simple here. Any ideas?
@singram I'm pretty sure this has to do with how IO is buffered in a docker container. If you add a puts "here" to the top of your snippet, you should find that both statements get printed.
Setting $stdout.sync = true near the top of the file solved the problem for me.
Thanks! That did the trick for me as well ($stdout.sync = true). Would be useful to have in the documentation somewhere for others.
Just wanted to share my thanks as well! $stdout.sync = true worked for puts. I also learned about stdout flush today. Cheers!
Most helpful comment
Setting
$stdout.sync = truenear the top of the file solved the problem for me.