Python, stuff I wished I'd known

Docker, Python, and Missing Terminal Output

I’ll be the first to admit that I make plenty of mistakes – I like to say that I’m wrong more than I’m right. File this under “stuff I wished I’d known sooner“.

Here’s the scenario: you’re dockerizing a python app that runs great locally, and the Docker version of the app refuses to do anything – it appears locked up, with no console output. Sound familiar? If so, then luckily the solution is pretty straightforward.

Let’s first look at the problematic app. It could be as simple as this:

Not much going on here, simply print a line of text to the console, and fall into an endless loop. The above could be made more complicated, perhaps the endless loop is processing some work queue, or checking a stock market feed, etc. In our example, we’ll stick with the basics, and just loop.

Running this app locally shows that it works just fine, so let’s dockerize it.

The above Dockerfile is also about as simple as things get. Copy the app to a path within the Docker environment, and run python on startup – the same command you’d use to run this app locally. Looks good? Let’s fire this up.

Nothing. Not a single output.

The solution here is actually to use the “-u” parameter when invoking Python. This flag disables output buffering:

With this modification the Dockerfile now looks like this:

That’s it! Running the app yields output, even when inside Docker, and hopefully this saved you some time.

Never miss an article! Subscribe to my newsletter and I'll keep you updated with the latest content.


About Jason

Jason is an experienced entrepreneur & software developer skilled in leadership, mobile development, data synchronization, and SaaS architecture. He earned his Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *