Docker-Py log with tail option is not working

567 Views Asked by At

I'm trying to perform an automation using docker package of python. In that I tried to read the logs with tails with below code, But it is showing nothing.

import docker
z=docker.from_env()
dkg = z.containers.get('<container_id>').logs(stream = True, follow = False, tail =10)
while True:
    line = next(dkg).decode("utf-8")
    print(line)

It would be helpful if you could identify the missing item in my code. Thanks in Advance for going through the question

1

There are 1 best solutions below

0
On

That code should work well.

The follow=True will keep waiting for more data; maybe that can help you.

Also, be sure that the data is being written to stdout.

I suggest changing the loop like this:

for line in dkg:
    print(line.decode())

That will avoid the StopIteration Exception.