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

Джейсон является опытным предпринимателем и разработчиком программного обеспечения, квалифицированным в области лидерства, мобильной разработки, синхронизации данных и архитектуры SaaS. Он получил степень бакалавра наук (B.S.) в области компьютерных наук в Университете штата Арканзас.
View all posts by Jason →

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *