0

    import docker
    import sys
    client= docker.from_env()
    app_name=sys.argv[1]
    def docker():
          tag=[]
          newList=[]
          #image=[]
          for container in client.containers.list(filters={"name":app_name}):
    
              a=container.image
              tag.append(a)
          print(tag[0])
         # for i in tag:
          #      newList.append(i.split(':')[0])
    
          #rint(newList)
    docker()

'' I'm getting output like this "<Image: 'gcr.io/ec2nf-256816/upf-fpm:it.r2.4.6'>" Could you please help me to get only "it.r2.4.6". ''

Haridvpsk
  • 91
  • 1
  • 11

1 Answers1

1
s = tag[0].__str__()
print(s)
print(s.split(':')[-1].split("'")[0])
Out: "<Image: 'gcr.io/ec2nf-256816/upf-fpm:it.r2.4.6'>"
Out: "it.r2.4.6"

What happens here?

s = tag[0].__str__() gets the Image tag as a string.

Then s.split(':') happens first which gives us:

['<Image', " 'gcr.io/ec2nf-256816/upf-fpm", "it.r2.4.6'>"]

Then we take the last element [-1] and split it by "'"

['it.r2.4.6', '>']

Then we just need to take the 0th element, since that's the target result.

"it.r2.4.6"
LPR
  • 400
  • 1
  • 8
  • The above is not working on my case. Please help me – Haridvpsk Apr 17 '21 at 07:48
  • Can you share the output for `dir(tag[0])`. We might be able to access the Image element of it. – LPR Apr 17 '21 at 07:56
  • print(tag[0]) output: – Haridvpsk Apr 17 '21 at 07:57
  • Our goal here is just to find a way to access this string 'gcr.io/ec2nf-256816/upf-fpm:it.r2.4.6' within the tag, then parsing should be very simple. Does `print(tag[0].Image)` give you that string or perhaps `tag[0]["Image"]`? – LPR Apr 17 '21 at 07:58
  • print(tag[0].Image )-->not getting expected result – Haridvpsk Apr 17 '21 at 08:01
  • print(tag[0]["Image"]) TypeError: 'Image' object is not subscriptable – Haridvpsk Apr 17 '21 at 08:02
  • If you print `type(tag[0])` we might be able to do a more directed search to figure out how to access the item. They certainly don't make it easy. – LPR Apr 17 '21 at 08:04
  • print(type(tag[0]))--> – Haridvpsk Apr 17 '21 at 08:05
  • it should have a list of accessible parameters which you can view if you `print(dir(tag[0]))`. One of those parameters should contain the "Image" info. – LPR Apr 17 '21 at 08:09
  • print(dir(tag[0])): ['__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'] – Haridvpsk Apr 17 '21 at 08:30
  • tag[0].getattribute('Image') sounds like what we're looking for. – LPR Apr 17 '21 at 08:31
  • this one is not working print(tag[0].getattribute('Image')) but print(tag[0].tag) O/P: > But i'm not getting how to get only "it.r2.4.6"..? – Haridvpsk Apr 17 '21 at 08:36
  • My final out put is this when I use print(tag[0].tags) python3 py.py upf-fpm ['gcr.io/ec2nf-256816/upf-fpm:it.r2.4.6'] – Haridvpsk Apr 17 '21 at 08:46
  • Is that a list with a string in it that you can access? – LPR Apr 17 '21 at 08:47
  • No, I could not able to access the string – Haridvpsk Apr 17 '21 at 08:48
  • It definitely feels like we're getting closer. I have to get some rest though, I'll comment if I have any ideas later. One last thought, try `help(tag[0].getattribute)` just to read what argument it is asking for. It seems like this should be the key to accessing the target string. Perhaps even just `tag[0].getattribute()` with no argument might work since there is only one item. – LPR Apr 17 '21 at 08:50
  • Sure ,I will try above solution and please let me know if you have any ideas. Thank you so much Mr LPR. – Haridvpsk Apr 17 '21 at 08:52
  • print(tag[0].getattribute()) AttributeError: 'Image' object has no attribute 'getattribute' – Haridvpsk Apr 17 '21 at 08:54
  • Not sure why I didn't realize this last night, but the bold items in the list `dir(tag[0])` are bold because of the formatting here. So `getattribute` is actually `__getattribute__`. That said, here is how `__getattribute__` works: https://medium.com/@satishgoda/python-attribute-access-using-getattr-and-getattribute-6401f7425ce6 and https://stackoverflow.com/questions/371753/how-do-i-implement-getattribute-without-an-infinite-recursion-error/371833#371833 so if `__getattribute__` was going to work, then tag[0].Image should have worked. – LPR Apr 17 '21 at 20:06
  • That said, you might try `tag[0].__str__()` or `tag[0].__repr__()` or `tag[0].__dict__['Image']` – LPR Apr 17 '21 at 20:07
  • im getting output is this : but how get only "it.r2.4.6" from the above output. – Haridvpsk Apr 18 '21 at 05:17
  • Is that the output from `tag[0].__str__()` and is it a string i.e. is `type(tag[0].__str__())` is a string? If so then `s = tag[0].__str__()` followed by the initial solution shown in the answer should work. Although, if it is still not a string, then I am stumped. – LPR Apr 18 '21 at 23:06
  • 1
    Awesome Thank you so much Mr. LPR . I have use s = tag[0].__str__() and its type is string. Then I use a=s.split(':')[-1].split("'")[0]) – Haridvpsk Apr 19 '21 at 04:26
  • Great! So glad to hear `tag[0].__str__().split(':')[-1].split("'")[0]` works! I'll update the answer so anyone who comes across this with a similar problem doesn't need to read our entire conversation XD – LPR Apr 20 '21 at 06:07