Skip to content

Instantly share code, notes, and snippets.

@Merwanski
Created November 24, 2021 13:43
Show Gist options
  • Save Merwanski/03664e1d6596c273a546be46048a9692 to your computer and use it in GitHub Desktop.
Save Merwanski/03664e1d6596c273a546be46048a9692 to your computer and use it in GitHub Desktop.
Docker container logs
# Docker container logs
import datetime
import docker
import time
import os
# how to run it
# python3 docker_logs.py
# TODO update the name for the correct one on the drone
NAME_CONTAINER = "portainer"
client = docker.from_env()
client.containers.list()
cmd_get_id = "docker ps -aqf 'name={}'".format(NAME_CONTAINER)
out = os.popen(cmd_get_id).read()
while len(out)==0:
print("[{}] waiting for container to start ...".format(time.time()))
out = os.popen(cmd_get_id).read()
time.sleep(1)
ID_CONTAINER = out[:-3]
container = client.containers.get(ID_CONTAINER)
"""
container.logs()
print(container.logs())
print(ID_CONTAINER)
"""
decoded_line = 'LOGS START ... '
for line in container.logs(stream=True):
if len(line)>1:
decoded_line = line.decode()
print(decoded_line)
else:
if line.decode() == "\n":
print(decoded_line)
decoded_line = ''
else:
decoded_line += line.decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment