2

I would like to get the container name by passing container id. I have tried below for getting that but unfortunately it didn't worked for me.

 import docker
 def get_container_details(self,container=123456789992):
     self.client = docker.from_env()
     print(self.client.containers.get(container))

May I know what is missing and how to get the container name from container ID

2 Answers 2

3

You were just a step away. Look at the snippet below,

>>> import docker
>>> client = docker.from_env()
>>> client.containers.list()
[<Container: 1c9276a9ca>]
>>> client.containers.get('1c9276a9ca').name
u'unruffled_mahavira'
Sign up to request clarification or add additional context in comments.

Comments

0

The list method of containers gives the Id of the container only. To get the corresponding name, you have to use name attribute, like below --

client = docker.from_env()

def get_all_container_list():
    containers = client.containers.list()
    for i in containers:
        print(i.name, i)

More to follow at official documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.