Home > Software design >  Docker sdk, proof if lable name exists
Docker sdk, proof if lable name exists

Time:07-22

I've got the current situation:

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

The problem is now, that it seems I can not access the data object in this fashion. I've tried some ways but can not get to the bottom of this.

Somebody an Idea how to access this object:

<Image: 'Tag1:Version', 'Tag2:Version'>

Thanks in advance

CodePudding user response:

If you use dir(image) you will get the structure of that object like next:

['__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']

Here, tags is what you needed like next:

test.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("===")

execution:

===
['__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
===

You could see the tags will get a python list of image name, so you could directly access that list.

  • Related