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

后端 未结 3 1755
既然无缘
既然无缘 2021-02-01 06:59

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}}         


        
相关标签:
3条回答
  • 2021-02-01 07:22

    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")}}'
    
    0 讨论(0)
  • 2021-02-01 07:29

    I built a docker ps pretty print function that can be put into your .bash_profile or .bashrc file that works somewhat like an alias for docker ps (with color output). @art-rock-guitar-superhero suggestions shows how to sort, but I've included this answer since typing the --format options and piping into a sort every time is a bit tedious.

    function docker () {
        if [[ "$@" == "ps -p" ]]; then
            command docker ps --all --format "{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Ports}}\t{{.Status}}" \
                | (echo -e "CONTAINER_ID\tNAMES\tIMAGE\tPORTS\tSTATUS" && cat) \
                | awk '{printf "\033[1;32m%s\t\033[01;38;5;95;38;5;196m%s\t\033[00m\033[1;34m%s\t\033[01;90m%s %s %s %s %s %s %s\033[00m\n", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10;}' \
                | column -s$'\t' -t \
                | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'
        else
            command docker "$@"
        fi
    }
    

    usage: $ docker ps -p.

    EDIT: I added suggestions from the comments from @BrianVosburgh. Also, I kept forgetting to type -p so I switched the flag for this function to be -a, which is my regular usage of docker ps.

    0 讨论(0)
  • 2021-02-01 07:38

    If it's enough to simply sort by output column, you can use the following:

     docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" | (read -r; printf "%s\n" "$REPLY"; sort -k 3 )
    

    I also added a code for skipping the table headers and sorting only ps output data.

    0 讨论(0)
提交回复
热议问题