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












8















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?










share|improve this question

























  • if you pipe and sort it? or have a look at Andy'answer from Docker Inspect Template Magic from Adrian Mouat container-solutions.com/docker-inspect-template-magic I guess you look for a docker only solution

    – user2915097
    Sep 12 '17 at 10:41











  • I don't think you can do anything golang template specific to do this. You need to build your own stuff for this. What may work is hitting the API and using jq to sort results and display. But not sure how easy that would be

    – Tarun Lalwani
    Sep 12 '17 at 11:38











  • from webgeodatavore.com/… I see Sort only value, not the object (return array) and jq '[.features.properties.pop_est] | sort' countries.geojson so I guess this is possible with jq (I do not have a docke ravailable at the moment, I will test later)

    – user2915097
    Sep 12 '17 at 11:48











  • and jq -R .|jq -sr 'sort_by(length)' sort by length from reddit.com/r/commandline/comments/3z9jff/jq_examples

    – user2915097
    Sep 12 '17 at 11:49











  • see also stackoverflow.com/questions/43930011/… for a similar basic docker command | sort and ther is an issue on that subject, see github.com/moby/moby/issues/31062

    – user2915097
    Sep 12 '17 at 13:24


















8















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?










share|improve this question

























  • if you pipe and sort it? or have a look at Andy'answer from Docker Inspect Template Magic from Adrian Mouat container-solutions.com/docker-inspect-template-magic I guess you look for a docker only solution

    – user2915097
    Sep 12 '17 at 10:41











  • I don't think you can do anything golang template specific to do this. You need to build your own stuff for this. What may work is hitting the API and using jq to sort results and display. But not sure how easy that would be

    – Tarun Lalwani
    Sep 12 '17 at 11:38











  • from webgeodatavore.com/… I see Sort only value, not the object (return array) and jq '[.features.properties.pop_est] | sort' countries.geojson so I guess this is possible with jq (I do not have a docke ravailable at the moment, I will test later)

    – user2915097
    Sep 12 '17 at 11:48











  • and jq -R .|jq -sr 'sort_by(length)' sort by length from reddit.com/r/commandline/comments/3z9jff/jq_examples

    – user2915097
    Sep 12 '17 at 11:49











  • see also stackoverflow.com/questions/43930011/… for a similar basic docker command | sort and ther is an issue on that subject, see github.com/moby/moby/issues/31062

    – user2915097
    Sep 12 '17 at 13:24
















8












8








8


3






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?










share|improve this question
















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?







docker






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '17 at 9:47







Thodi

















asked Sep 12 '17 at 9:54









ThodiThodi

148113




148113













  • if you pipe and sort it? or have a look at Andy'answer from Docker Inspect Template Magic from Adrian Mouat container-solutions.com/docker-inspect-template-magic I guess you look for a docker only solution

    – user2915097
    Sep 12 '17 at 10:41











  • I don't think you can do anything golang template specific to do this. You need to build your own stuff for this. What may work is hitting the API and using jq to sort results and display. But not sure how easy that would be

    – Tarun Lalwani
    Sep 12 '17 at 11:38











  • from webgeodatavore.com/… I see Sort only value, not the object (return array) and jq '[.features.properties.pop_est] | sort' countries.geojson so I guess this is possible with jq (I do not have a docke ravailable at the moment, I will test later)

    – user2915097
    Sep 12 '17 at 11:48











  • and jq -R .|jq -sr 'sort_by(length)' sort by length from reddit.com/r/commandline/comments/3z9jff/jq_examples

    – user2915097
    Sep 12 '17 at 11:49











  • see also stackoverflow.com/questions/43930011/… for a similar basic docker command | sort and ther is an issue on that subject, see github.com/moby/moby/issues/31062

    – user2915097
    Sep 12 '17 at 13:24





















  • if you pipe and sort it? or have a look at Andy'answer from Docker Inspect Template Magic from Adrian Mouat container-solutions.com/docker-inspect-template-magic I guess you look for a docker only solution

    – user2915097
    Sep 12 '17 at 10:41











  • I don't think you can do anything golang template specific to do this. You need to build your own stuff for this. What may work is hitting the API and using jq to sort results and display. But not sure how easy that would be

    – Tarun Lalwani
    Sep 12 '17 at 11:38











  • from webgeodatavore.com/… I see Sort only value, not the object (return array) and jq '[.features.properties.pop_est] | sort' countries.geojson so I guess this is possible with jq (I do not have a docke ravailable at the moment, I will test later)

    – user2915097
    Sep 12 '17 at 11:48











  • and jq -R .|jq -sr 'sort_by(length)' sort by length from reddit.com/r/commandline/comments/3z9jff/jq_examples

    – user2915097
    Sep 12 '17 at 11:49











  • see also stackoverflow.com/questions/43930011/… for a similar basic docker command | sort and ther is an issue on that subject, see github.com/moby/moby/issues/31062

    – user2915097
    Sep 12 '17 at 13:24



















if you pipe and sort it? or have a look at Andy'answer from Docker Inspect Template Magic from Adrian Mouat container-solutions.com/docker-inspect-template-magic I guess you look for a docker only solution

– user2915097
Sep 12 '17 at 10:41





if you pipe and sort it? or have a look at Andy'answer from Docker Inspect Template Magic from Adrian Mouat container-solutions.com/docker-inspect-template-magic I guess you look for a docker only solution

– user2915097
Sep 12 '17 at 10:41













I don't think you can do anything golang template specific to do this. You need to build your own stuff for this. What may work is hitting the API and using jq to sort results and display. But not sure how easy that would be

– Tarun Lalwani
Sep 12 '17 at 11:38





I don't think you can do anything golang template specific to do this. You need to build your own stuff for this. What may work is hitting the API and using jq to sort results and display. But not sure how easy that would be

– Tarun Lalwani
Sep 12 '17 at 11:38













from webgeodatavore.com/… I see Sort only value, not the object (return array) and jq '[.features.properties.pop_est] | sort' countries.geojson so I guess this is possible with jq (I do not have a docke ravailable at the moment, I will test later)

– user2915097
Sep 12 '17 at 11:48





from webgeodatavore.com/… I see Sort only value, not the object (return array) and jq '[.features.properties.pop_est] | sort' countries.geojson so I guess this is possible with jq (I do not have a docke ravailable at the moment, I will test later)

– user2915097
Sep 12 '17 at 11:48













and jq -R .|jq -sr 'sort_by(length)' sort by length from reddit.com/r/commandline/comments/3z9jff/jq_examples

– user2915097
Sep 12 '17 at 11:49





and jq -R .|jq -sr 'sort_by(length)' sort by length from reddit.com/r/commandline/comments/3z9jff/jq_examples

– user2915097
Sep 12 '17 at 11:49













see also stackoverflow.com/questions/43930011/… for a similar basic docker command | sort and ther is an issue on that subject, see github.com/moby/moby/issues/31062

– user2915097
Sep 12 '17 at 13:24







see also stackoverflow.com/questions/43930011/… for a similar basic docker command | sort and ther is an issue on that subject, see github.com/moby/moby/issues/31062

– user2915097
Sep 12 '17 at 13:24














3 Answers
3






active

oldest

votes


















6














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 "%sn" "$REPLY"; sort -k 3 )


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






share|improve this answer

































    4














    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")}}'





    share|improve this answer

































      3














      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_IDtNAMEStIMAGEtPORTStSTATUS" && cat)
      | awk '{printf "33[1;32m%st33[01;38;5;95;38;5;196m%st33[00m33[1;34m%st33[01;90m%s %s %s %s %s %s %s33[00mn", $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.






      share|improve this answer





















      • 1





        Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m%st... should read ...196m%st....

        – Brian Vosburgh
        Nov 13 '18 at 17:41








      • 1





        Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

        – Brian Vosburgh
        Nov 13 '18 at 17:59













      • @BrianVosburgh, yes, thanks, I removed the extraneous backslash. I'm not sure why, but it had no impact on the output. Also, the extra stuff to sort by second column (key) of name, is a nice suggestion as well, and so I've updated the original answer with your sort command.

        – David John Coleman II
        Nov 21 '18 at 13:23













      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46173298%2fhow-to-sort-or-order-results-docker-ps-format%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      6














      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 "%sn" "$REPLY"; sort -k 3 )


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






      share|improve this answer






























        6














        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 "%sn" "$REPLY"; sort -k 3 )


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






        share|improve this answer




























          6












          6








          6







          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 "%sn" "$REPLY"; sort -k 3 )


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






          share|improve this answer















          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 "%sn" "$REPLY"; sort -k 3 )


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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 23 '18 at 5:47









          Rob

          23.8k115575




          23.8k115575










          answered Apr 23 '18 at 3:43









          Art-Rock Guitar SuperheroArt-Rock Guitar Superhero

          8117




          8117

























              4














              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")}}'





              share|improve this answer






























                4














                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")}}'





                share|improve this answer




























                  4












                  4








                  4







                  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")}}'





                  share|improve this answer















                  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")}}'






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 2 '18 at 0:32

























                  answered Sep 12 '17 at 13:45









                  Jinna BaluJinna Balu

                  1,2871118




                  1,2871118























                      3














                      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_IDtNAMEStIMAGEtPORTStSTATUS" && cat)
                      | awk '{printf "33[1;32m%st33[01;38;5;95;38;5;196m%st33[00m33[1;34m%st33[01;90m%s %s %s %s %s %s %s33[00mn", $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.






                      share|improve this answer





















                      • 1





                        Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m%st... should read ...196m%st....

                        – Brian Vosburgh
                        Nov 13 '18 at 17:41








                      • 1





                        Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

                        – Brian Vosburgh
                        Nov 13 '18 at 17:59













                      • @BrianVosburgh, yes, thanks, I removed the extraneous backslash. I'm not sure why, but it had no impact on the output. Also, the extra stuff to sort by second column (key) of name, is a nice suggestion as well, and so I've updated the original answer with your sort command.

                        – David John Coleman II
                        Nov 21 '18 at 13:23


















                      3














                      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_IDtNAMEStIMAGEtPORTStSTATUS" && cat)
                      | awk '{printf "33[1;32m%st33[01;38;5;95;38;5;196m%st33[00m33[1;34m%st33[01;90m%s %s %s %s %s %s %s33[00mn", $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.






                      share|improve this answer





















                      • 1





                        Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m%st... should read ...196m%st....

                        – Brian Vosburgh
                        Nov 13 '18 at 17:41








                      • 1





                        Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

                        – Brian Vosburgh
                        Nov 13 '18 at 17:59













                      • @BrianVosburgh, yes, thanks, I removed the extraneous backslash. I'm not sure why, but it had no impact on the output. Also, the extra stuff to sort by second column (key) of name, is a nice suggestion as well, and so I've updated the original answer with your sort command.

                        – David John Coleman II
                        Nov 21 '18 at 13:23
















                      3












                      3








                      3







                      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_IDtNAMEStIMAGEtPORTStSTATUS" && cat)
                      | awk '{printf "33[1;32m%st33[01;38;5;95;38;5;196m%st33[00m33[1;34m%st33[01;90m%s %s %s %s %s %s %s33[00mn", $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.






                      share|improve this answer















                      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_IDtNAMEStIMAGEtPORTStSTATUS" && cat)
                      | awk '{printf "33[1;32m%st33[01;38;5;95;38;5;196m%st33[00m33[1;34m%st33[01;90m%s %s %s %s %s %s %s33[00mn", $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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 28 '18 at 4:02

























                      answered Oct 20 '18 at 18:21









                      David John Coleman IIDavid John Coleman II

                      350410




                      350410








                      • 1





                        Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m%st... should read ...196m%st....

                        – Brian Vosburgh
                        Nov 13 '18 at 17:41








                      • 1





                        Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

                        – Brian Vosburgh
                        Nov 13 '18 at 17:59













                      • @BrianVosburgh, yes, thanks, I removed the extraneous backslash. I'm not sure why, but it had no impact on the output. Also, the extra stuff to sort by second column (key) of name, is a nice suggestion as well, and so I've updated the original answer with your sort command.

                        – David John Coleman II
                        Nov 21 '18 at 13:23
















                      • 1





                        Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m%st... should read ...196m%st....

                        – Brian Vosburgh
                        Nov 13 '18 at 17:41








                      • 1





                        Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

                        – Brian Vosburgh
                        Nov 13 '18 at 17:59













                      • @BrianVosburgh, yes, thanks, I removed the extraneous backslash. I'm not sure why, but it had no impact on the output. Also, the extra stuff to sort by second column (key) of name, is a nice suggestion as well, and so I've updated the original answer with your sort command.

                        – David John Coleman II
                        Nov 21 '18 at 13:23










                      1




                      1





                      Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m%st... should read ...196m%st....

                      – Brian Vosburgh
                      Nov 13 '18 at 17:41







                      Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m%st... should read ...196m%st....

                      – Brian Vosburgh
                      Nov 13 '18 at 17:41






                      1




                      1





                      Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

                      – Brian Vosburgh
                      Nov 13 '18 at 17:59







                      Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}'

                      – Brian Vosburgh
                      Nov 13 '18 at 17:59















                      @BrianVosburgh, yes, thanks, I removed the extraneous backslash. I'm not sure why, but it had no impact on the output. Also, the extra stuff to sort by second column (key) of name, is a nice suggestion as well, and so I've updated the original answer with your sort command.

                      – David John Coleman II
                      Nov 21 '18 at 13:23







                      @BrianVosburgh, yes, thanks, I removed the extraneous backslash. I'm not sure why, but it had no impact on the output. Also, the extra stuff to sort by second column (key) of name, is a nice suggestion as well, and so I've updated the original answer with your sort command.

                      – David John Coleman II
                      Nov 21 '18 at 13:23




















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46173298%2fhow-to-sort-or-order-results-docker-ps-format%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                      Alcedinidae

                      RAC Tourist Trophy