我以前从未使用过它,但我只是将build() 放在print() 中,我发现它返回了一些值。
(<Image: 'ubuntu:latest'>, <itertools._tee object at 0x7f836bea2b00>)
所以我开始使用print(...)、print(type(...))、print(dir(...)) 对其进行测试
因为我看到了itertools这个词,所以我尝试将它与for-loop一起使用。
这段代码
import docker
client = docker.from_env()
result = client.images.build(path='.', quiet=False)
#print(result)
print(result[0], type(result[0]))
print(dir(result[0])
for item in result[1]:
print(item)
给我
<Image: 'ubuntu:latest'> <class 'docker.models.images.Image'>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'attrs', 'client', 'collection', 'history', 'id', 'id_attribute', 'labels', 'reload', 'save', 'short_id', 'tag', 'tags']
{'stream': 'Step 1/1 : FROM ubuntu'}
{'stream': '\n'}
{'stream': ' ---> 7e0aa2d69a15\n'}
{'aux': {'ID': 'sha256:7e0aa2d69a153215c790488ed1fcec162015e973e49962d438e18249d16fa9bd'}}
{'stream': 'Successfully built 7e0aa2d69a15\n'}
它显示字典,所以使用key,value 我可以格式化它(并过滤它)
import docker
client = docker.from_env()
result = client.images.build(path='.')#, quiet=False)
#print(result)
#print(result[0], type(result[0]))
#print(dir(result[0]))
for item in result[1]:
#print(item)
for key, value in item.items():
#print(key, ':', value)
if key == 'stream':
text = value.strip()
if text:
print(text)
这给了我
Step 1/1 : FROM ubuntu
---> 7e0aa2d69a15
Successfully built 7e0aa2d69a15