docker / docker-py

A Python library for the Docker Engine API
https://docker-py.readthedocs.io/
Apache License 2.0
6.77k stars 1.66k forks source link

Docker Logs Stream #2984

Open ruscott2009 opened 2 years ago

ruscott2009 commented 2 years ago

I want to stream a running containers stdout logs to my interpreter however the logs don't appear when the container is running. When the container finishes all the logs are printed at once. Any ideas where I can start to troubleshoot this issue? I’m using dockerpy 5.0.3 and Docker Desktop 4.7.1. Thank you in advance.

client = docker.from_env() container = client.containers.run( "ptcloud2ptcloud:latest", command=["/bin/bash", "-c", cmd], volumes=volumes, detach=True, remove=True, stdout=True, stderr=True, cpu_count=7, mem_limit='16g',

)

print("container submitted") for line in container.logs(stream=True): print(line.strip())

naufalafif commented 1 year ago

might be late. but can you provide what cmd variable is on your code above

it's working from my side

import docker

client = docker.from_env()

cmd = 'while true; do date && sleep 1s; done'
container = client.containers.run(
    image='ubuntu',
    detach=True,
    command=['/bin/bash', '-c', cmd],
    stderr=True,
    stdout=True,
)

for line in container.logs(stream=True):
    print(line.decode().strip())

Screenshot 2023-04-05 213928