How to pipe stdout from docker-run in dockerpy

Viewed 1460

When I run my docker image normally like so

docker run -v ..... {DOCKER_IMAGE}

I get my log in the terminal as expected. But running like so:

docker_client = docker.from_env()
container  = docker_client.containers.run(DOCKER_IMAGE, None, environment=[f'foo={self.profile}'], 
volumes={...,}, auto_remove=True, stderr=True, stdout=True)
# No logs show up

Seems to suppress logs. How can I get the log output while it's running? Can I stream the docker container run?

I can do this

docker_client.containers.run(.... detach=True)
for line in container.logs(stream=True): 
    print(line)

But this loses whether it's a debug/info/warning/error

2 Answers

What about setting stream=True?

Haven't tested it, but in the docs it says:

stream (bool) – If true and detach is false, return a log generator instead of a string. Ignored if detach is true. Default: False.

Please, give it a try and tell me how it went.

Update: After googling for "stream", I found a similar question that may help you here

Related