2

I am following aiodocker example by replacing echo "hello world" with python -c 'print("hello world")', but the logs are not showing.

Below solutions (use PYTHONUNBUFFERED=1, flush=True, -u flag) do not help:

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()

What else can be wrong?

aboev
  • 332
  • 2
  • 9
  • `'Cmd': ['python', '-c', 'print("hello world")']` does: `'Cmd': ['python', '-u', 'print("hello world")']` make any difference? – Cow Dec 30 '21 at 09:12
  • @user56700 `-u` flag does not help either – aboev Dec 30 '21 at 09:19

1 Answers1

0

Please wait for the started container to finish before getting logs:

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

Another option is using follow=True parameter for logs fetching:

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

It can be applied to the running container instance.

Andrew Svetlov
  • 16,730
  • 8
  • 66
  • 69