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 app.py 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.

About Jason

Jason est un entrepreneur expérimenté et développeur de logiciels qualifié dans le leadership, le développement mobile, la synchronisation des données et l’architecture SaaS. Il a obtenu son baccalauréat ès sciences (B.S.) en informatique de l’Université d’État de l’Arkansas.
View all posts by Jason →

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *