Sinatra: Docker STDOUT missing when using sinatra

Created on 28 May 2016  路  4Comments  路  Source: sinatra/sinatra

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?

Most helpful comment

Setting $stdout.sync = true near the top of the file solved the problem for me.

All 4 comments

@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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

za3k picture za3k  路  5Comments

AvailCat picture AvailCat  路  4Comments

mediafinger picture mediafinger  路  7Comments

paulmenzel picture paulmenzel  路  4Comments

fullofcaffeine picture fullofcaffeine  路  7Comments