如何在docker python API中传输日志?
我正在使用docker python API从Dockerfile构build一个图像。
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)
操作成功完成,但是我无法stream式传输日志。 该API说:
返回一个可以迭代的阻塞生成器来检索生成输出
当stream = True时。
我怎样才能得到这些日志在Python?
可以使用docker-py中给出的低级API来完成Docker构build日志的stream式处理 ,如下所示,
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)
文档状态…
如果您想要获取构build的原始输出,请在低级API中使用build()方法。
你尝试过吗?