2017-04-21 28 views
0

我正在使用docker python API从Dockerfile构建图像。如何在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说:

返回阻塞发生器可以遍历检索建设 输出,因为它发生

当流=真。

如何在Python中获取这些日志?

回答

3

流泊坞窗构建日志可以使用docker-py给出的低级别的API如下进行,

 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) 
0

docs状态...

如果你想构建的原始输出,使用低级别API构建()方法。

你试过吗?