76

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?

Jakuje
  • 24,773
  • 12
  • 69
  • 75
corey
  • 761
  • 1
  • 5
  • 3

6 Answers6

119
docker ps --format "{{.Names}}"
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189
cleong
  • 7,242
  • 4
  • 31
  • 40
17

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 '/'.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
12

You could do that with this command inspired by this question:

docker inspect --format='{{.Name}}' $(sudo docker ps -aq --no-trunc)
Community
  • 1
  • 1
Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
10

You can also do it using awk:

docker ps -a | awk '{print $NF}'

$NF indicates the last column of the input, which in docker ps -a command is Names.

Lefteris Xris
  • 900
  • 1
  • 11
  • 21
  • This, however, also outputs the column header - which is not desired – Javier Arias Mar 19 '19 at 13:15
  • as @ascendants suggested, to exclude column headers use: `docker ps -a | awk '{ if( FNR>1 ) { print $NF }'` FNR *indicates to skip the first entry (the column header)* – Lefteris Xris May 06 '20 at 09:46
10

You can use following command to display names of container

docker ps -a --format "table {{.ID}}\t{{.Names}}"

Here is reference link for more information about ps command https://docs.docker.com/engine/reference/commandline/ps/

Maulik Parmar
  • 857
  • 10
  • 15
3

My requirement is get the container user name to login into the container (I need use docker exec -it --user to login container). Use docker inspect + container id and grep user or name then you can get the Container User Name and login into the container.

Example:

    # docker inspect 5791b95933ef |grep -i user
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
kain
  • 214
  • 2
  • 4