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

后端 未结 3 1811
既然无缘
既然无缘 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: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.

提交回复
热议问题