skip to Main Content
docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                           NAMES
f56084659b2a        nginx:alpine        "/docker-entrypoint.…"   35 seconds ago      Up 33 seconds       0.0.0.0:3456->3456/tcp, 0.0.0.0:38080->80/tcp   xenodochial_perlman

what is the cmd to actually count num of ports from PORTS column in 1 line?

2

Answers


  1. Ports in itself is a long string, even though it looks like a list. You can see that by using {{ json . }} or {{ json .Ports }}.

    You can work around that by using the split function and then calling len on the resulting slice.

    docker ps --format '{{ len (split .Ports ",") }}'
    

    Furthermore, you can still show it in a table, but the column name for that len column will be a number. I have not found out yet how to rename columns with the table function. I did ask one time in the slack group, but didn’t get an answer to that.

    docker ps --format 'table {{.Names }}t{{ len (split .Ports ",") }}'
    
    Login or Signup to reply.
  2. Using inspect :

    docker inspect -f "{{len .NetworkSettings.Ports}}" f56084659b2a
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search