Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to display container names in docker stats?

Tags:

docker

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 
like image 692
jamlee Avatar asked Jun 09 '15 12:06

jamlee


People also ask

How do I get docker container metrics?

You can use the docker stats command to live stream a container's runtime metrics. The command supports CPU, memory usage, memory limit, and network IO metrics. The docker stats reference page has more details about the docker stats command.

How do I check container stats?

If you need more detailed information about a container's resource usage, use the /containers/(id)/stats API endpoint. On Linux, the Docker CLI reports memory usage by subtracting cache usage from the total memory usage.


2 Answers

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}}" 
like image 120
jamshid Avatar answered Sep 22 '22 17:09

jamshid


docker stats $(docker ps | awk '{if(NR>1) print $NF}') 
like image 39
user2915097 Avatar answered Sep 22 '22 17:09

user2915097