48

I want to display state of all running container, so I could achieve it like the following:

docker stats $(docker ps -q)

CONTAINER           CPU %               MEM USAGE/LIMIT       MEM %               NET I/O
04cdc87ba3cf        0.03%               468.8 MiB/3.784 GiB   12.10%              6.827 KiB/10.2 KiB
7d30fcbd8b36        0.09%               88.09 MiB/3.784 GiB   2.27%               28.23 KiB/289.2 KiB
a09ef63b2c59        97.94%              271.5 MiB/512 MiB     53.03%              3.644 MiB/190.2 KiB
a29681c1980f        0.10%               9.066 MiB/3.784 GiB   0.23%               2.538 KiB/648 B

but the column container is only showing the container id. I need the container name though. For example:

docker stats lrlcms_web_1

CONTAINER           CPU %               MEM USAGE/LIMIT      MEM %               NET I/O
lrlcms_web_1        0.09%               88.1 MiB/3.784 GiB   2.27%               28.85 KiB/289.2 KiB

So how do I get all the container names? Just for:

docker stats `all container's name'

For example:

docker stats lrlcms_db_1 lrlcms_redis_1

CONTAINER           CPU %               MEM USAGE/LIMIT       MEM %               NET I/O
lrlcms_db_1         0.05%               450.3 MiB/3.784 GiB   11.62%              8.737 KiB/10.2 KiB
lrlcms_redis_1      0.08%               7.383 MiB/3.784 GiB   0.19%               4.448 KiB/648 B
4
  • docker stats is not able to display more than this at the moment (by the way it would be handy to sort on any column) , the doc docs.docker.com/reference/commandline/cli/#stats says also Note: If you want more detailed information about a container's resource usage, use the API endpoint. Jun 9, 2015 at 12:44
  • Maybe if instead of passing the ids, you pass a list of container names, something in the idea of docker stats $(docker ps | awk ' NR > 1 {print $2}' (untested, but you get the idea, I do not have access at a docker prompt at the moment) Jun 9, 2015 at 12:59
  • try docker stats $(docker ps | awk '{if(NR>1) print $2}') Jun 9, 2015 at 13:05
  • 1
    should be default with Docker 17.10 version: github.com/moby/moby/issues/20973
    – Tilo
    Mar 21, 2018 at 18:28

4 Answers 4

96

Or, using plain "docker ps" instead of "awk"... note "--format" is normally used with "docker inspect":

docker stats $(docker ps --format '{{.Names}}')

2017-02-12 See manat's answer below (https://stackoverflow.com/a/42060599/72717). Docker 1.13.0 "stats" can display the container name in "--format":

docker stats --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"
2
  • 1
    Best possible answer. Teacher two things at a time :-) Sep 17, 2016 at 10:34
  • You can also sort the statistics. And also get rid of some container name prefix-suffix, e.g: docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}" |sed -E 's/^local.cluster.([a-z1-2\-]+).1(.*)/\1\2/'|sed -E 's/NAME {17}/NAME /'| sort -k 2 -h -r (assuming container names have default prefix "local-cluster-" and default suffix "-1) Sample sorts by CPU% ascending.
    – gilyen
    Mar 10 at 14:41
52
docker stats $(docker ps | awk '{if(NR>1) print $NF}')
1
  • Hello, Thanks so much, could you please provide an explanation?
    – vcmorini
    Jun 12 at 13:10
11

Since docker 1.13.0 (#27797), there's a format option which support container name. So you can run it like this:

docker stats --format "table {{.Name}}\t{{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}"

See Docker Formatting for full details.

1
  • 1
    Please fix dead link
    – Nate Zaugg
    Dec 12, 2018 at 3:10
4

A bit hacky, but works:

docker stats $(docker ps | tail -n +2 | awk '{print $NF}')

tail -n +2 is there to remove docker ps header line, and finally awk '{print $NF}' prints the last column (i.e. container name) for every input line

1
  • This worked for me better than the actual answer, but in my case I was removing all the containers (I had A LOT of them). so my command was docker rm -f $(docker ps -a | tail -n +2 | awk '{print $NF}')
    – Fadi
    Dec 6, 2016 at 20:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.