我正在尝试编写一个 Python 脚本。从这个输出中我尝试只获取名称
python 447
原文标题 :I am trying to write a Python script..From this output I am try to get only names
代码
import docker
client = docker.from_env()
input=client.images.list()
print(input)
for row in input:
print(row)
在此处输入图像描述
回复
我来回复-
aaossa 评论
也许您想访问每个图像 ID(文档):
for image in client.images.list(): print(image.id)
2年前 -
CryptoFool 评论
我不确定您所说的“仅名称”是什么意思。docker 图像有两种主要的识别方式:
- 唯一 ID – 图像的散列,或者是完整散列的前 10 个十六进制数字的简短版本
- 与该映像关联的零个或多个存储库/标签对
您从
docker.from_env().images
返回的内容包括每个本地图像的所有这些信息。要显示此信息,您可以执行以下操作:import docker client = docker.from_env() images = client.images.list() for image in images: print("ID: " + image.short_id.split(':')[1]) # Strips the "sha256:" from the front of the short id for tag in image.tags: print(" Tag: " + tag)
这将产生如下所示的内容:
ID: 3815f96140 Tag: inletfetch/fetch:202203030831 ID: 1d426c2230 Tag: 123445678704.dkr.ecr.us-west-2.amazonaws.com/harmony/epicenter:latest Tag: 445678704390.dkr.ecr.us-east-1.amazonaws.com/harmony/epicenter:latest Tag: epicenter:latest ID: 50d8edf8ca ID: 68a3a7e6b9 ID: 51b01658d1
请注意,有些图像有多个与之关联的 repo/tag 对,而另一些则没有 repo/tag 对。因此,如果您只打印每一对,您将获得一些图像的多行,而其他图像则根本没有行。
将每个 repo/tag 对视为图像的人类可读“名称”并不罕见,并且您经常希望忽略没有此类标识符的图像。如果您只想打印这些人类友好的“名称”,您可以这样做:
import docker client = docker.from_env() images = client.images.list() for image in images: for tag in image.tags: print(tag)
对于相同的图像列表,它会给你这个:
inletfetch/fetch:202203030831 123445678704.dkr.ecr.us-west-2.amazonaws.com/harmony/epicenter:latest 445678704390.dkr.ecr.us-east-1.amazonaws.com/harmony/epicenter:latest epicenter:latest
2年前