Docker 内线程内的 tqdm 不显示进度条

Gia*_*uca 6 python progress-bar docker docker-compose tqdm

从 Docker 容器内部来看,tqdm在其自己的线程上运行时似乎出现了一些问题。

我尝试了其他进度条(例如alive-progress),但它们都失败了。当在容器外部运行时,相同的代码可以完美地工作。这似乎是 Docker 特有的问题,很可能是一个docker-compose问题,但我无法发现问题。有什么建议吗?

编辑:docker run -it console_test 有效。肯定是配置有问题docker-compose.yml

重现步骤:

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)
Run Code Online (Sandbox Code Playgroud)

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"]
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我这样运行整个事情:

docker-compose up --build console_test
Run Code Online (Sandbox Code Playgroud)

在 for 循环期间,不显示栏。完成并被print调用后,结果如下:

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.
Run Code Online (Sandbox Code Playgroud)

编辑

部分解决方案:

之后,我发现docker-compose run似乎up可以解决问题。但不知道为什么。