dockersorting

How to sort or order results docker ps --format?


I haven't found any way to order my results when using docker ps

In my case I want to order by .Ports

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

How do I order the result?


Solution

  • Format and Order docker ps

    List containers

    docker ps
    

    Synopsis

    docker ps [--format="TEMPLATE"]
    
    --format="TEMPLATE"
      Pretty-print containers using a Go template.
      Valid placeholders:
         .ID - Container ID
         .Image - Image ID
         .Command - Quoted command
         .CreatedAt - Time when the container was created.
         .RunningFor - Elapsed time since the container was started.
         .Ports - Exposed ports.
         .Status - Container status.
         .Size - Container disk size.
         .Names - Container names.
         .Labels - All labels assigned to the container.
         .Label - Value of a specific label for this container. For example {{.Label "com.docker.swarm.cpu"}}
         .Mounts - Names of the volumes mounted in this container.
    

    Display containers with their commands

    docker ps --format "{{.ID}}: {{.Command}}"
    

    Display containers with their labels in a table

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

    Display containers with their node label in a table

    docker ps --format 'table {{.ID}}\t{{(.Label "com.docker.swarm.node")}}'