Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Docker Container Names

This command gives me a list of running container IDs:

docker ps -q 

Is there a command to get the list of names of the containers?

like image 471
corey Avatar asked Aug 07 '15 22:08

corey


People also ask

What is the docker container name?

When a container is created using Docker, Docker provides a way to name the container using the the --name <container_name> flag. However, if no name is provided by the user while creating/running a Docker container, Docker automatically assigns the container a name.

Is docker container name unique?

Because Docker container names must be unique, you cannot scale a service beyond 1 container if you have specified a custom name.


2 Answers

docker ps --format "{{.Names}}" 
like image 166
cleong Avatar answered Sep 22 '22 01:09

cleong


You can combine docker ps with docker inspect, as I mentioned before in "How do you list containers in Docker.io?":

docker inspect --format='{{.Name}}' $(sudo docker ps -aq --no-trunc) docker inspect --format='{{.Name}}' $(sudo docker ps -aq --no-trunc) | cut -c2- 

As commented by Chris Stryczynski, it will print names with a '/' as a prefix.

vagrant@master:~$ docker inspect --format='{{.Name}}' $(sudo docker ps -aq --no-trunc) /k8s_kubernetes-dashboard_kubernetes-dashboard-d9d8f48bc-vz59c_kube-system_b2abc584-730a_0 /k8s_POD_kubernetes-dashboard-d9d8f48bc-vz59c_kube-system_b2abc584-_0 /k8s_metrics-server_metrics-server-6fbfb84cdd-sjrgr_kube-system_e147bf91-7218-11e8-8266_0 /k8s_POD_metrics-server-6fbfb84cdd-sjrgr_kube-system_e147bf91-7218-11e8-8266-00155d380143_0 

From moby/moby issue 6705:

Inspect exposes the inner details of how docker is handling the container.
Names are prefixed with their parent and / == "the docker daemon".
That is why every name will have this prefixed.
This will be more important when nesting and multihost come into play.
The / is correct for the inspect command.

Hence the | cut -c2-.

More recently (June 2017), there is a proposal (moby/moby issue 29997) for removing the '/':

the leading slash is there for historical reasons (mainly because of the legacy container-linking)

So far (June 2018), no PR has been fully implemented to get rid of the leading '/'.

like image 20
VonC Avatar answered Sep 25 '22 01:09

VonC