Proper log streaming

This commit is contained in:
Einar Forselv 2019-04-18 07:22:59 +02:00
parent 756da55018
commit 01f7cef709
1 changed files with 13 additions and 11 deletions

View File

@ -1,4 +1,5 @@
import os import os
import sys
import time import time
import docker import docker
@ -14,21 +15,22 @@ def run(image: str = None, command: str = None, volumes: dict = None,
image, image,
command, command,
labels=labels, labels=labels,
auto_remove=True, # auto_remove=True,
detach=True, detach=True,
environment=enviroment, environment=enviroment,
volumes=volumes, volumes=volumes,
working_dir=os.getcwd(), working_dir=os.getcwd(),
tty=True,
) )
print("Backup process container:", container.name) print("Backup process container:", container.name)
logs = container.logs(stdout=True, stderr=True, stream=True) log_generator = container.logs(stdout=True, stderr=True, stream=True, follow=True)
try: with open('backup.log', 'w') as fd:
while True: for line in log_generator:
time.sleep(3) line = line.decode()
for line in logs: fd.write(line)
print(line.decode(), end='') print(line, end='')
# Raises requests.exceptions.HTTPError if continer is dead
container.top() container.reload()
except Exception as ex: print("ExitCode", container.attrs['State']['ExitCode'])
print("Container stopped") container.remove()