【问题标题】:Python logs not showing when run via aiodocker通过 aiodocker 运行时未显示 Python 日志
【发布时间】:2021-12-30 08:48:36
【问题描述】:

我正在关注aiodocker example,将echo "hello world" 替换为python -c 'print("hello world")',但未显示日志。

以下解决方案(使用PYTHONUNBUFFERED=1flush=True-u 标志)无济于事:

Python app does not print anything when running detached in docker

Why doesn't my docker actually log anything?

import asyncio
import aiodocker

async def run_container():
    docker = aiodocker.Docker()
    container = await docker.containers.create_or_replace(
        config={
            'Cmd': ['python', '-c', 'print("hello world")'],
            'Image': 'python:3.8.2-buster',
        },
        name="testing"
    )
    await container.start()
    logs = await container.log(stdout=True, stderr=True)
    print(''.join(logs))
    await container.delete(force=True)
    await docker.close()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run_container())
    loop.close()

还有什么问题?

【问题讨论】:

  • 'Cmd': ['python', '-c', 'print("hello world")'] 是否:'Cmd': ['python', '-u', 'print("hello world")'] 有什么不同?
  • @user56700 -u 标志也无济于事

标签: python docker logging python-asyncio


【解决方案1】:

请等待启动的容器完成后再获取日志:

await container.start()
await container.wait()

另一个选项是使用follow=True 参数来获取日志:

async for chunk in container.log(stdout=True, stderr=True, follow=True):
    print(chunk)

它可以应用于正在运行的容器实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多