Docker-sdk,证明标签名称是否存在



我得到了当前的情况:

import docker
import platform
java-base = False
client = docker.from_env()
images = client.images.list(filters={"label":"architecture"
+platform.machine()})
for image in images:
    if "java-base" in image:
        java-base = True
if java-base == False:
    #build this image

现在的问题是,我似乎无法以这种方式访问数据对象。我已经尝试了一些方法,但还是无法弄清真相。

某人有一个想法如何访问这个对象:

<图片:"Tag1:版本","Tag2:版本">

提前感谢

如果使用dir(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']

在这里,tags是你下一个需要的东西:

测试.py:

import docker
client = docker.from_env()
images = client.images.list()
for image in images:
    print("===")
    print(dir(image))
    print(image)  # this is what you get
    print(image.tags) # use this instead
    print(type(image.tags))
    for tag in image.tags:
        print(tag)
    print("===")

执行:

===
['__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']
<Image: 'docker.elastic.co/elasticsearch/elasticsearch-platinum:6.1.4'>
['docker.elastic.co/elasticsearch/elasticsearch-platinum:6.1.4']
<class 'list'>
docker.elastic.co/elasticsearch/elasticsearch-platinum:6.1.4
===
===
['__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']
<Image: 'ubuntu:17.04', 'ubuntu:zesty'>
['ubuntu:17.04', 'ubuntu:zesty']
<class 'list'>
ubuntu:17.04
ubuntu:zesty
===

您可以看到tags将获得图像名称的python list,因此您可以直接访问该列表。

最新更新