6

As part of docker automation am trying to automate certain docker operation using python docker package. Well I have implemented the most of requirement successfully, But could not find an equivalent method for

docker logs <container-id>

My implementation:

import docker
client = docker.APIClient()
client.attach(container, stdout=True, stderr=True, stream=False, logs=True, demux=False)

However it does not give result what am expecting. Kindly help me to identify what is missing in the implementation. Thanks in advance for going through this question

#UPDATE

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)

Well I have replace my implementation with above one by referring to link. Although it does not work when tail option added. On adding the tail command it is showing blank without any error. Any hint on getting tailed results

5
  • 1
    Can you try this solution (stackoverflow.com/a/62825364/8295943) Commented Mar 22, 2021 at 18:56
  • @MustafaGüler I have modified question after trying with above reference. Kindly go through it Commented Mar 23, 2021 at 5:04
  • So without tail does it work? Commented Mar 23, 2021 at 5:18
  • @MustafaGüler Yes its work. But it hard to work large files. When I went through documentation found this logs(container, stdout=True, stderr=True, stream=False, timestamps=False, tail='all', since=None, follow=None, until=None)¶ Commented Mar 23, 2021 at 6:30
  • @MustafaGüler My ultimate requirement is to docker logs contaner | tail -100 | grep <pattern> Commented Mar 23, 2021 at 6:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.