我正在使用Docker python API从Dockerfile构建一个镜像。
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说:
返回一个阻塞生成器,您可以迭代它以检索生成输出
当stream=True时。
如何在python中获取这些日志?
8条答案
按热度按时间ttisahbt1#
根据文档,映像构建现在返回一个包含映像和构建日志的元组
第一项是构建的图像的Image对象。第二项是构建日志的生成器,作为JSON解码的对象。
并相应修改@havock解决方案:
mtb9vblg2#
可以使用docker-py中给出的低级API来流式传输docker构建日志,如下所示:
jgwigjjp3#
您可以使用底层API客户端。
build()
函数将返回一个生成器,您可以迭代该生成器以获取构建日志的块。生成器将生成一个包含JSON对象的字符串,您可以在其上调用
json.loads()
,或者您可以在build()
函数中使用decode=True
参数,该函数将为您执行此操作。一旦你从生成的字典中获取了
'stream'
键,你可能只需要print()
它,但是如果你需要将它发送到一个日志记录器,那么逐行发送可能会更好,因为接收到的块将包含多行。这种代码的一个选项如下:
46scxncf4#
Docker低级别构建能够直接将输出解码为字典。为此使用
decode=True
。我使用mashumaro将字典转换为数据类。如果你喜欢类型提示,这很有用。要使用数据类,你至少需要python3.7。
安装mashumaro:
数据类:
建立您的形象:
希望这能帮上忙。
iih3973s5#
如果你对上面的答案有问题,我可以用这个来得到我需要的:
3xiyfsfu6#
使用较新版本的python docker sdk,您仍然可以使用高级API函数,因为它们在需要时返回
[object_created, generator]
形式的元组。这已经在
python 3.10
中进行了测试,运行docker==4.2.2
和click==8.0.3
以下是我正在使用的函数,用于记录我正在构建的cli的docker输出,基于这个答案:
示例用法:
sg24os4d7#
我遇到了同样的问题,这个解决方案对我很有效:
environment={"PYTHONUNBUFFERED": "1"}
kulphzqa8#
docs状态…
如果您想获得构建的原始输出,请使用低级API中的build()方法。
你试过吗