question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unable to stream container logs

See original GitHub issue

Using the following code, I’ve been unable to stream logs without the generator blocking infinitely:

import docker

base_url = 'unix://var/run/docker.sock'
client = docker.Client(base_url=base_url,
                       version='1.14',
                       timeout=10)

command = 'echo hello world'
container = client.create_container('ubuntu:14.04', command=command)

client.start(container)
log_stream = client.logs(container, stream=True)

for line in log_stream:
    print(line)

but waiting for the container to complete and reading the logs without streaming works just fine. Am I consuming the output incorrectly, or is there another issue here?

I’m currently using Docker 1.2.0 on Arch, with Python 3.4.1.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
lnxpycommented, Sep 16, 2020

Hey @kvchen, I found a way to fix this problem. In spite I’m creating a Django container, you just need to create a loop in the streaming process and print the log line like this:

print('Running the container..')
container = client.containers.run('something', detach=True)
process = container.logs(stream=True, follow=True)
print('Stream logging the container..')
for line in process:
    print(line)

It creates a streaming loop in order to print each request that comes to the container. The output goes to be:

$ python test.py
Running the container..
Stream logging the container..

Then, create some requests to the endpoint and it shows…

$ python test.py
Running the container..
Stream logging the container..
b'Watching for file changes with StatReloader\n'
b"Invalid HTTP_HOST header: '172.17.0.3:8000'. You may need to add '172.17.0.3' to ALLOWED_HOSTS.\n"
b'Bad Request: /\n'
b'[16/Sep/2020 10:27:26] "GET / HTTP/1.1" 400 58366\n'
b"Invalid HTTP_HOST header: '172.17.0.3:8000'. You may need to add '172.17.0.3' to ALLOWED_HOSTS.\n"
b'Bad Request: /favicon.ico\n'
b'[16/Sep/2020 10:27:26] "GET /favicon.ico HTTP/1.1" 400 58286\n'

Hope it works for you. It works properly on my project DBMQ.

0reactions
leej3commented, Aug 6, 2020

I ran up against this. I wonder is it the stream kwarg of run that is confusing people (it confused me). I would expect the following to be a non-blocking call and return a generator:

client.containers.run(
        'alpine',
        "/bin/sh -c 'echo hello;sleep 10; echo bye bye'",
        detach=False,
        stream=True,
        )

What I wanted was actually:

 container = client.containers.run(
    'alpine',
    "/bin/sh -c 'echo hello;sleep 10; echo bye bye'",
    detach=True,
    )
for line in container.logs(stream=True):
    print(line.decode("utf-8"))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are my Amazon ECS container logs not delivered to ...
Your Amazon ECS container logs aren't delivered to CloudWatch Logs due to one or more of the following reasons: The awslogs log driver...
Read more >
View container logs - Docker Documentation
The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format...
Read more >
How to Live Tail Docker Logs - Papertrail
In Docker jargon, we refer to creating a continuous stream of log output as tailing logs. To tail the logs for our container,...
Read more >
How to view Docker logs to troubleshoot containers
The only thing you'll need to view Docker container logs is Docker deployed to a machine. It doesn't matter what the platform is,...
Read more >
Docker Log Collection Troubleshooting Guide - Datadog Docs
This status means that the Agent is unable to find a log file for a given container. To resolve this issue, check that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found