tqdm inside a thread inside Docker doesn't show progress bar

Viewed 978

From inside a Docker container, it seems that tqdm is having some trouble showing up when run on its own thread.

I tried other progress bars (eg. alive-progress), but they all fail. The same code works flawlessly when ran outside of the container. It seems a Docker-specific issue, most likely a docker-compose one, but I cannot spot the problem. Any tip?

Edit: docker run -it console_test works. It's definitely a problem in the configuration of docker-compose.yml.

Steps to reproduce:

console_test/console.py:

import threading
import time
import tqdm
import sys

print("Starting the test")


def job():
    for i in tqdm.tqdm(range(50)):
        time.sleep(0.1)
        sys.stdout.flush()  # this has no effect
        # print("Hi")  # this is printed though!
    print("Finished.")


job_thread = threading.Thread(target=job)
job_thread.start()

while 1:
    time.sleep(1)

console_test/Dockerfile:

# Using official python runtime base image
FROM python:3.8.9

RUN pip install tqdm

COPY . .

ENV PYTHONUNBUFFERED=1  # removing this has no effect
ENV PYTHONIOENCODING=UTF-8  # removing this has no effect

ENTRYPOINT ["python", "console.py"]

docker-compose.yml:

version: "3.2"
services:
    console_test:
        build: ./console_test
        stdin_open: true # docker run -i // removing it has no effect
        tty: true        # docker run -t // removing it has no effect

And I run the whole thing like this:

docker-compose up --build console_test

During the for-loop, the bar is not shown. After it finishes and print is called, this is the result:

Attaching to rotonda_console_test_1
console_test_1   | Starting the test
100% 50/50 [00:05<00:00,  9.97it/s]
console_test_1   | Finished.

EDIT

Partial solution:

Following this, I discovered that docker-compose run instead of up seems to fix the issue. No idea why though.

0 Answers
Related