如何在docker python API中流式传输日志? [英] How to stream the logs in docker python API?
本文介绍了如何在docker python API中流式传输日志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用docker python API从Dockerfile构建映像。
I am building an image from a Dockerfile using the docker python API.
import os
import sys
import os.path
import docker
client = docker.from_env()
try:
here = os.path.dirname(__file__)
no_cache = False
dockerfile = os.path.join(here, 'app', 'nextdir')
image = client.images.build(path=dockerfile, tag='app:v.2.4', nocache=no_cache, stream=True)
操作成功完成,但是我无法流式传输日志。该API表示:
The operation finishes successfully, however I was not able to stream the logs. The API says:
返回一个阻塞生成器,您可以对其进行迭代以在生成
输出时进行检索
Return a blocking generator you can iterate over to retrieve build output as it happens
。
如何在python中获取这些日志?
How can I get these logs in python?
推荐答案
可以使用 docker-py 如下,
here = os.path.dirname(__file__)
dockerfile = os.path.join(here, 'app', 'nextdir')
docker_client = docker.APIClient(base_url='unix://var/run/docker.sock')
generator = docker_client.build(path=dockerfile, tag='app:v.2.4', rm=True)
while True:
try:
output = generator.__next__
output = output.strip('\r\n')
json_output = json.loads(output)
if 'stream' in json_output:
click.echo(json_output['stream'].strip('\n'))
except StopIteration:
click.echo("Docker image build complete.")
break
except ValueError:
click.echo("Error parsing output from docker image build: %s" % output)
这篇关于如何在docker python API中流式传输日志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文