sorting - How to sort or order results docker ps --format? - Stack Overflow (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (Stack Overflow) (https://stackoverflow.com/opensearch.xml) (https://stackoverflow.com/questions/46173298/how-to-sort-or-order-results-docker-ps-format) (site logo) By clicking “Sign up”, you agree to our (https://stackoverflow.com/legal/terms-of-service/public) terms of service and acknowledge you have read our (https://stackoverflow.com/legal/privacy-policy) privacy policy . Sign up with Google Sign up with GitHub OR Email Password (8+ characters (at least 1 letter & 1 number)) Sign up Already have an account? (https://stackoverflow.com/users/login) Log in Skip to main content (https://stackoverflow.com/) Stack Overflow (https://stackoverflow.co/) About Products (https://stackoverflow.co/teams/) For Teams (Search…) (Click to show search) (https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f46173298%2fhow-to-sort-or-order-results-docker-ps-format) Log in (https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3a%2f%2fstackoverflow.com%2fquestions%2f46173298%2fhow-to-sort-or-order-results-docker-ps-format) Sign up Let's set up your homepage Select a few topics you're interested in: python javascript c# reactjs java android html flutter c++ node.js typescript css r php angular next.js spring-boot machine-learning sql excel ios azure docker Or search from our full list: (Search) Next You’ll be prompted to create an account to view your personalized homepage. (https://stackoverflow.com/) Home (https://stackoverflow.com/questions) Questions (https://stackoverflow.ai/) AI Assist Labs (https://stackoverflow.com/tags) Tags (https://stackoverflow.com/beta/challenges) Challenges (https://chat.stackoverflow.com/rooms/259507/stack-overflow-lobby) Chat (https://stackoverflow.blog/contributed?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=so-blog&utm_content=experiment-articles) Articles (https://stackoverflow.com/users) Users (https://stackoverflow.com/jobs?source=so-left-nav) Jobs (https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav) Companies Collectives Communities for your favorite technologies. (https://stackoverflow.com/collectives-all) Explore all Collectives Teams Ask questions, find answers and collaborate at work with Stack Overflow for Teams. (https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) Try Teams for free (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) Explore Teams (Illustration of upvote icon after it is clicked) Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. Upvoting indicates when questions and answers are useful. (https://stackoverflow.com/help/whats-reputation) What's reputation and how do I get it? Instead, you can save this post to reference later. Save this post for later Not now (Illustration of upvote icon after it is clicked) Thanks for your vote! You now have 5 free votes weekly. Free votescount toward the total vote score does not give reputation to the author Continue to help good content that is interesting, well-researched, and useful, rise to the top! To gain full voting privileges, (https://stackoverflow.com/help/whats-reputation) earn reputation . Got it! Go to help center to learn more (https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png?v=c78bd457575a) (https://stackoverflow.com/questions/46173298/how-to-sort-or-order-results-docker-ps-format) How to sort or order results docker ps --format? [closed] (https://stackoverflow.com/questions/ask) Ask Question (2017-09-12 09:54:12Z) Asked 8 years, 1 month ago Modified (https://stackoverflow.com/questions/46173298/how-to-sort-or-order-results-docker-ps-format?lastactivity) (2025-07-08 23:41:13Z) 3 months ago (Viewed 27,177 times) Viewed 27k times This question shows research effort; it is useful and clear 35 (This question does not show any research effort; it is unclear or not useful) Save this question. (https://stackoverflow.com/posts/46173298/timeline) Show activity on this post. Closed. This question is (https://stackoverflow.com/help/closed-questions) not about programming or software development . It is not currently accepting answers. This question does not appear to be about (https://stackoverflow.com/help/on-topic) a specific programming problem, a software algorithm, or software tools primarily used by programmers . If you believe the question would be on-topic on (https://stackexchange.com/sites) another Stack Exchange site , you can leave a comment to explain where the question may be able to be answered. Closed (2025-07-08 23:41:30Z) 4 months ago . The community reviewed whether to reopen this question (2025-07-08 23:41:55Z) 4 months ago and left it closed: Original close reason(s) were not resolved (https://stackoverflow.com/posts/46173298/edit) 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? (https://stackoverflow.com/questions/tagged/docker) (show questions tagged 'docker') docker (https://stackoverflow.com/questions/tagged/sorting) (show questions tagged 'sorting') sorting (https://stackoverflow.com/q/46173298) (Short permalink to this question) Share (https://stackoverflow.com/posts/46173298/edit) Improve this question Follow Follow this question to receive notifications (https://stackoverflow.com/posts/46173298/revisions) (show all edits to this post) edited (2025-07-08 23:41:13Z) Jul 8 at 23:41 (https://stackoverflow.com/users/16217248/cplus) (CPlus's user avatar) (https://stackoverflow.com/users/16217248/cplus) CPlus (reputation score) 5,103 (48 gold badges) 48 48 gold badges (33 silver badges) 33 33 silver badges (76 bronze badges) 76 76 bronze badges asked (2017-09-12 09:54:12Z) Sep 12, 2017 at 9:54 (https://stackoverflow.com/users/8247069/theo) (Theo's user avatar) (https://stackoverflow.com/users/8247069/theo) Theo Theo (reputation score) 2,970 (5 gold badges) 5 5 gold badges (28 silver badges) 28 28 silver badges (59 bronze badges) 59 59 bronze badges 5 (number of 'useful comment' votes received) 2 if you pipe and sort it? or have a look at Andy'answer from Docker Inspect Template Magic from Adrian Mouat (http://container-solutions.com/docker-inspect-template-magic/) container-solutions.com/docker-inspect-template-magic I guess you look for a docker only solution user2915097 – (https://stackoverflow.com/users/2915097/user2915097) (32,384 reputation) user2915097 2017-09-12 10:41:21 +00:00 Commented (2017-09-12 10:41:21Z, License: CC BY-SA 3.0) Sep 12, 2017 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 – (https://stackoverflow.com/users/2830850/tarun-lalwani) (147,168 reputation) Tarun Lalwani 2017-09-12 11:38:08 +00:00 Commented (2017-09-12 11:38:08Z, License: CC BY-SA 3.0) Sep 12, 2017 at 11:38 from (http://webgeodatavore.com/jq-json-manipulation-command-line-with-geojson.html) 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 – (https://stackoverflow.com/users/2915097/user2915097) (32,384 reputation) user2915097 2017-09-12 11:48:12 +00:00 Commented (2017-09-12 11:48:12Z, License: CC BY-SA 3.0) Sep 12, 2017 at 11:48 and jq -R .|jq -sr 'sort_by(length)[]' sort by length from (https://www.reddit.com/r/commandline/comments/3z9jff/jq_examples/) reddit.com/r/commandline/comments/3z9jff/jq_examples user2915097 – (https://stackoverflow.com/users/2915097/user2915097) (32,384 reputation) user2915097 2017-09-12 11:49:07 +00:00 Commented (2017-09-12 11:49:07Z, License: CC BY-SA 3.0) Sep 12, 2017 at 11:49 see also (https://stackoverflow.com/questions/43930011/sort-by-memory-usage-in-docker-stats) (sort by memory usage in docker stats) stackoverflow.com/questions/43930011/… for a similar basic docker command | sort and ther is an issue on that subject, see (https://github.com/moby/moby/issues/31062) github.com/moby/moby/issues/31062 user2915097 – (https://stackoverflow.com/users/2915097/user2915097) (32,384 reputation) user2915097 2017-09-12 13:24:08 +00:00 Commented (2017-09-12 13:24:08Z, License: CC BY-SA 3.0) Sep 12, 2017 at 13:24 (this comment was edited 1 time) (Use comments to ask for more information or suggest improvements. Avoid answering questions in comments.) Add a comment | (Expand to show all comments on this post) 6 Answers 6 Sorted by: (https://stackoverflow.com/questions/46173298/how-to-sort-or-order-results-docker-ps-format?answertab=scoredesc#tab-top) Reset to default (scoredesc) Highest score (default) (trending) Trending (recent votes count more) (modifieddesc) Date modified (newest first) (createdasc) Date created (oldest first) This answer is useful 41 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/49973096/timeline) Show activity on this post. 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. (https://stackoverflow.com/a/49973096) (Short permalink to this answer) Share (https://stackoverflow.com/posts/49973096/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/49973096/revisions) (show all edits to this post) edited (2018-04-23 05:47:00Z) Apr 23, 2018 at 5:47 (https://stackoverflow.com/users/563532/rob) (Rob's user avatar) (https://stackoverflow.com/users/563532/rob) Rob (Moderator) ♦ (reputation score 27,442) 27.4k (16 gold badges) 16 16 gold badges (89 silver badges) 89 89 silver badges (103 bronze badges) 103 103 bronze badges answered (2018-04-23 03:43:44Z) Apr 23, 2018 at 3:43 (https://stackoverflow.com/users/2588010/igor-maximov) (Igor Maximov's user avatar) (https://stackoverflow.com/users/2588010/igor-maximov) Igor Maximov Igor Maximov (reputation score) 606 (1 gold badge) 1 1 gold badge (5 silver badges) 5 5 silver badges (11 bronze badges) 11 11 bronze badges Sign up to request clarification or add additional context in comments. 1 Comment Add a comment (https://stackoverflow.com/users/2977468/user2977468) user2977468 (https://stackoverflow.com/users/2977468/user2977468) user2977468 (2021-03-08T22:56:48.013Z) Over a year ago (Edited 1 time) For those wondering, the final statement of sort -k 3 indicates that the Docker container results will be sorted ascending by the third column of the format template. So in this example, the container list will be sorted by the respective container's exposed ports. 2021-03-08T22:56:48.013Z+00:00 7 Reply This answer is useful 11 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/52908761/timeline) Show activity on this post. 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). (https://stackoverflow.com/users/2588010/art-rock-guitar-superhero) @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 (https://stackoverflow.com/users/1566345/brian-vosburgh) @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 . (https://stackoverflow.com/a/52908761) (Short permalink to this answer) Share (https://stackoverflow.com/posts/52908761/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/52908761/revisions) (show all edits to this post) edited (2018-11-28 04:02:39Z) Nov 28, 2018 at 4:02 answered (2018-10-20 18:21:30Z) Oct 20, 2018 at 18:21 (https://stackoverflow.com/users/5567854/david-john-coleman-ii) (David John Coleman II's user avatar) (https://stackoverflow.com/users/5567854/david-john-coleman-ii) David John Coleman II David John Coleman II (reputation score) 639 (8 silver badges) 8 8 silver badges (16 bronze badges) 16 16 bronze badges 7 Comments Add a comment (https://stackoverflow.com/users/1566345/brian-vosburgh) Brian Vosburgh (https://stackoverflow.com/users/1566345/brian-vosburgh) Brian Vosburgh (2018-11-13T17:41:35.647Z) Over a year ago (Edited 1 time) Nice function. : ) But I think there is an extraneous backslash in the 'awk' pattern: ...196m\%s\t... should read ...196m%s\t... . 2018-11-13T17:41:35.647Z+00:00 1 Reply (https://stackoverflow.com/users/1566345/brian-vosburgh) Brian Vosburgh (https://stackoverflow.com/users/1566345/brian-vosburgh) Brian Vosburgh (2018-11-13T17:59:27.08Z) Over a year ago (Edited 1 time) Also, you could sort the table with something like this: docker ps -p | awk 'NR<2{print $0;next}{print $0 | "sort --key=2"}' 2018-11-13T17:59:27.08Z+00:00 1 Reply (https://stackoverflow.com/users/5567854/david-john-coleman-ii) David John Coleman II (https://stackoverflow.com/users/5567854/david-john-coleman-ii) David John Coleman II (2018-11-21T13:23:25.567Z) Over a year ago (Edited 1 time) @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. 2018-11-21T13:23:25.567Z+00:00 0 Reply (https://stackoverflow.com/users/2294168/ser) Ser (https://stackoverflow.com/users/2294168/ser) Ser (2021-12-01T12:43:58.82Z) Over a year ago You cannot sort on the status, as this field contains Up 2 days , Shutdown about a day ago or whatever. It just works if all the containers were created on the same moment and they have all the same status. I create another answer that describes how to generate the ISO date from the Status value and sort on it. 2021-12-01T12:43:58.82Z+00:00 0 Reply (https://stackoverflow.com/users/5567854/david-john-coleman-ii) David John Coleman II (https://stackoverflow.com/users/5567854/david-john-coleman-ii) David John Coleman II (2021-12-02T17:19:40.403Z) Over a year ago @Ser the proposed solution sorts by Name only, it's not configurable to sort by Status intentionally to make it a simple use for general purpose. 2021-12-02T17:19:40.403Z+00:00 1 Reply Add a comment | Show 2 more comments This answer is useful 8 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/46178116/timeline) Show activity on this post. 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")}}' (https://stackoverflow.com/a/46178116) (Short permalink to this answer) Share (https://stackoverflow.com/posts/46178116/edit) Improve this answer Follow Follow this answer to receive notifications (https://stackoverflow.com/posts/46178116/revisions) (show all edits to this post) edited (2020-07-25 13:42:34Z) Jul 25, 2020 at 13:42 (https://stackoverflow.com/users/4099593/bhargav-rao) (Bhargav Rao's user avatar) (https://stackoverflow.com/users/4099593/bhargav-rao) Bhargav Rao (reputation score 52,557) 52.6k (29 gold badges) 29 29 gold badges (130 silver badges) 130 130 silver badges (142 bronze badges) 142 142 bronze badges answered (2017-09-12 13:45:24Z) Sep 12, 2017 at 13:45 (https://stackoverflow.com/users/4348824/jinna-baalu) (Jinna Baalu's user avatar) (https://stackoverflow.com/users/4348824/jinna-baalu) Jinna Baalu Jinna Baalu (reputation score) 7,957 (40 silver badges) 40 40 silver badges (51 bronze badges) 51 51 bronze badges 6 Comments Add a comment (https://stackoverflow.com/users/217844/ssc) ssc (https://stackoverflow.com/users/217844/ssc) ssc (2019-07-07T19:49:52.72Z) Over a year ago what is "$REPLY" ? 2019-07-07T19:49:52.72Z+00:00 3 Reply (https://stackoverflow.com/users/205481/bradym) bradym (https://stackoverflow.com/users/205481/bradym) bradym (2020-05-16T05:21:02.53Z) Over a year ago Read is a bash builtin that reads from stdin and stores the value in a variable. If you don't provide a name for the variable it uses REPLY. (https://ss64.com/bash/read.html) ss64.com/bash/read.html 2020-05-16T05:21:02.53Z+00:00 0 Reply (https://stackoverflow.com/users/3266847/benjamin-w) Benjamin W. (https://stackoverflow.com/users/3266847/benjamin-w) Benjamin W. (2020-06-09T02:33:40.403Z) Over a year ago This answer didn't contain the sorting part at all, and just copied it from the (https://stackoverflow.com/a/49973096/3266847) this answer , currently second highest voted, without attribution? 2020-06-09T02:33:40.403Z+00:00 13 Reply (https://stackoverflow.com/users/4348824/jinna-baalu) Jinna Baalu (https://stackoverflow.com/users/4348824/jinna-baalu) Jinna Baalu (2020-06-10T13:29:11.96Z) Over a year ago @BenjaminW. So great that you found the answer I copied from, which I was unaware. I copied in Nov 2018, answered in Sep 2017. 2020-06-10T13:29:11.96Z+00:00 0 Reply (https://stackoverflow.com/users/2977468/user2977468) user2977468 (https://stackoverflow.com/users/2977468/user2977468) user2977468 (2021-03-08T22:53:05.523Z) Over a year ago (Edited 1 time) This does NOT sort. Consider concise and capable (https://stackoverflow.com/questions/46173298/how-to-sort-or-order-results-docker-ps-format/49973096#49973096) answer here . 2021-03-08T22:53:05.523Z+00:00 14 Reply Add a comment | Show 1 more comment This answer is useful 1 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/79694485/timeline) Show activity on this post. I usually want to sort the output by container name, which appears at the very end of each line, so I use a sed substitution to add a temporary copy of the desired column (the last column) to the front of the line, sort the new lines, then remove that temporary column: docker ps | sed -E 's/(^.* )([^ ]*$)/\2 \1\2/' | LC_ALL=C sort | cut -d' ' -f2- The regex (^.* )([^ ]*$) matches the last group of characters on each line that doesn't contain a space ([^ ]*$ ), then captures that group (that regex is enclosed in parentheses), as well as all of the characters preceding it ((^.* ) ). The expression \2 \1\2 is then the second group (the container name field), a space, and the original line including the original container name field. Setting LC_ALL=C in the environment of sort ensures that fields are sorted lexicographically, so that "NAME" still appears as the column heading in the final output, rather than these headings appearing as some row other than the first row. cut -d' ' -f2- then strips out the temporary leading "NAME" column from the final output: it prints fields/columns from number 2 onwards, where fields are delimited by space characters. This is a pretty convenient way of sorting by container name, since I don't need to do something more cumbersome, like strip out extraneous spaces between columns, use sort to sort by a particular column, and then add back the spaces to format the columns nicely again. I have this pipeline as a shell alias called docker-pss : alias docker-pss="docker ps | sed -E 's/(^.* )([^ ]*$)/\2 \1\2/' | LC_ALL=C sort | cut -d' ' -f2-" Since you want to sort by ports, and your docker ps format string has this as the last column, you should be able to use this pipeline to achieve that. However, if you want to sort by the actual port numbers that appear in this column (e.g. 80 in ::1->80/tcp or 80/tcp ), you'll have to match on the value that appears between the possible arrow (-> ) and guaranteed slash (/ ) in this field. Here's how you could do that using a sed expression of a similar nature to the one above, using the default output of docker ps , not using a custom format string: { echo -n 'PORT ' # Add extra heading for temporary column. This won't appear in the final output docker ps } | sed -E 's/(^.*(->| ))([0-9]+)(\/.*$)/\3 \1\3\4/' | sort -nt' ' -k1,1 | cut -d' ' -f2- This won't correctly sort lines with an empty "PORTS" field. Instead, it will strip their container ID from the output and sort them as if their container ID was their port number. If you want to exclude such lines from the output, you can filter them out with grep before running sed: { echo -n 'PORT ' docker ps } | grep -E '^PORT |(->| )[0-9]+/' | # Filter out lines that lack a port number, except the heading line sed -E 's/(^.*(->| ))([0-9]+)(\/.*$)/\3 \1\3\4/' | sort -nt' ' -k1,1 | cut -d' ' -f2- (https://stackoverflow.com/a/79694485) (Short permalink to this answer) Share (https://stackoverflow.com/posts/79694485/edit) Improve this answer Follow Follow this answer to receive notifications answered (2025-07-08 15:30:27Z) Jul 8 at 15:30 (https://stackoverflow.com/users/9996911/jivan-pal) (Jivan Pal's user avatar) (https://stackoverflow.com/users/9996911/jivan-pal) Jivan Pal Jivan Pal (reputation score) 190 (10 bronze badges) 10 10 bronze badges Comments Add a comment This answer is useful 0 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/69058374/timeline) Show activity on this post. Prettify Docker ps output open terminal run export command to export Env Variable you can customise according to desired output needed export FORMAT="ID\t{{.ID}}\nNAME\t{{.Names}}\nIMAGE\t{{.Image}}\nPORTS\t{{.Ports}}\nCOMMAND\t{{.Command}}\nCREATED\t{{.CreatedAt}}\nSTATUS\t{{.Status}}\n" run command docker ps --format="$FORMAT" e.g Output khizerrehan@khizerrehans-MacBook-Pro ~ % docker ps --format="$FORMAT " ID 9bfbfe65dce1 NAME happy_joliot IMAGE nginx:alpine PORTS 0.0.0.0:80->80/tcp, :::80->80/tcp COMMAND "/docker-entrypoint.…" CREATED 2021-09-05 00:20:45 +0500 PKT STATUS Up 4 minutes (https://stackoverflow.com/a/69058374) (Short permalink to this answer) Share (https://stackoverflow.com/posts/69058374/edit) Improve this answer Follow Follow this answer to receive notifications answered (2021-09-04 19:28:51Z) Sep 4, 2021 at 19:28 (https://stackoverflow.com/users/6618218/khizerrehandev) (khizerrehandev's user avatar) (https://stackoverflow.com/users/6618218/khizerrehandev) khizerrehandev khizerrehandev (reputation score) 1,577 (17 silver badges) 17 17 silver badges (15 bronze badges) 15 15 bronze badges Comments Add a comment This answer is useful 0 (This answer is not useful) Save this answer. (Loading when this answer was accepted…) (https://stackoverflow.com/posts/70184613/timeline) Show activity on this post. I know this is not strictly related to the OP question, but if you want to sort on the Status date, you need to support date that are relatives : Up 2 days ago , Shutdown about a day ago etc.This bash script generates an ISO date from the Status value, then we sort on the ISO date and finally we remove the ISO date. # 1- List all containers # As I'm working with swarm, you may want to adapt to your docker cli usage. # Use "docker ps --format '{{ json . }}" to see available fields # 2- add a column that translate "Shutdown 2 days ago" into ISO date. Also supports "Shutdown about a day ago". # 3- sort on that date # 4- remove the ISO date and add colors # 5- make the output readable docker stack ps --format '{{ .CurrentState }}\t{{ .Name }}\t{{.Node}}\t{{.Image}}' demo \ | awk -F'\t' '{system("date --rfc-3339=seconds -u -d \"$(printf \"" $1 "\" | cut -d \" \" -f2- | sed \"s/about//g\") \" | tr -d \"\n\"") ; printf "\t%s\t%s\t%s\t%s\t%s\n", $1, $2, $3, $4, $5;}' \ | sort -r \ | awk -F'\t' '{ printf "\033[32m%s\033[39m\t%s\t%s\t%s\n", $2, $3, $4, $5, $6;}' \ | column -o " " -t -s $'\t' It generates something like that :(https://i.sstatic.net/4Z6Q4.png) (ordered containers by date) (https://stackoverflow.com/a/70184613) (Short permalink to this answer) Share (https://stackoverflow.com/posts/70184613/edit) Improve this answer Follow Follow this answer to receive notifications answered (2021-12-01 12:55:53Z) Dec 1, 2021 at 12:55 (https://stackoverflow.com/users/2294168/ser) (Ser's user avatar) (https://stackoverflow.com/users/2294168/ser) Ser Ser (reputation score) 2,920 (27 silver badges) 27 27 silver badges (33 bronze badges) 33 33 bronze badges Comments Add a comment Start asking to get answers Find the answer to your question by asking. (https://stackoverflow.com/questions/ask) Ask question Explore related questions (https://stackoverflow.com/questions/tagged/docker) (show questions tagged 'docker') docker (https://stackoverflow.com/questions/tagged/sorting) (show questions tagged 'sorting') sorting See similar questions with these tags. The Overflow Blog (https://stackoverflow.blog/2025/10/24/your-runbooks-are-obsolete-in-the-age-of-agents/?cb=1) Your runbooks are obsolete in the age of agents Featured on Meta (Meta Stack Exchange) (https://meta.stackexchange.com/questions/412992/a-first-look-stack-overflow-redesign?cb=1) A First Look: Stack Overflow Redesign (Meta Stack Exchange) (https://meta.stackexchange.com/questions/413210/community-asks-sprint-announcement-october-november-2025?cb=1) Community Asks Sprint Announcement - October/November 2025 (Meta Stack Overflow) (https://meta.stackoverflow.com/questions/435293/upcoming-opinion-based-questions-alpha-experiment-on-stack-overflow?cb=1) Upcoming opinion-based questions alpha experiment on Stack Overflow (https://chat.stackoverflow.com/) (11 users active in 9 rooms the last 60 minutes) 11 people chatting Community activity Last 1 hr Users online activity 8112 users online Number of questions asked 15 questions Number of questions that received an answer 14 answers Number of comments posted 40 comments Number of upvotes on posts 160 upvotes Popular tags Popular tags (https://stackoverflow.com/questions/tagged/c#) c# (https://stackoverflow.com/questions/tagged/android) android (https://stackoverflow.com/questions/tagged/windows) windows (https://stackoverflow.com/questions/tagged/python) python (https://stackoverflow.com/questions/tagged/javascript) javascript (https://stackoverflow.com/questions/tagged/rust) rust Popular unanswered question Popular unanswered question (https://stackoverflow.com/questions/79799821) What does np.ones_like represent in a quiver plot of a differential equation? (https://stackoverflow.com/questions/tagged/numpy) numpy (https://stackoverflow.com/questions/tagged/matplotlib) matplotlib (https://stackoverflow.com/questions/tagged/visualization) visualization (https://stackoverflow.com/questions/tagged/differential-equations) differential-equations (https://stackoverflow.com/users/31754345) (User avatar) (https://stackoverflow.com/users/31754345) Youness Ansarey 1 (Sunday, October 26, 2025 at 3:22:32 AM UTC) 11 hours ago Linked (https://stackoverflow.com/questions/43930011/sort-by-memory-usage-in-docker-stats?lq=1) (Question score (upvotes - downvotes)) 48 (https://stackoverflow.com/questions/43930011/sort-by-memory-usage-in-docker-stats?noredirect=1&lq=1) Sort by memory usage in docker stats (https://stackoverflow.com/questions/55454155/docker-ps-a-sort-by-date?lq=1) (Question score (upvotes - downvotes)) 3 (https://stackoverflow.com/questions/55454155/docker-ps-a-sort-by-date?noredirect=1&lq=1) Docker ps -a sort by date Related (https://stackoverflow.com/questions/43930011/sort-by-memory-usage-in-docker-stats?rq=3) (Question score (upvotes - downvotes)) 48 (https://stackoverflow.com/questions/43930011/sort-by-memory-usage-in-docker-stats?rq=3) Sort by memory usage in docker stats (https://stackoverflow.com/questions/50667371/docker-ps-output-formatting-list-only-names-of-running-containers?rq=3) (Question score (upvotes - downvotes)) 187 (https://stackoverflow.com/questions/50667371/docker-ps-output-formatting-list-only-names-of-running-containers?rq=3) 'docker ps' output formatting: list only names of running containers (https://stackoverflow.com/questions/50887431/how-to-format-docker-ps-output-to-not-use-relative-times?rq=3) (Question score (upvotes - downvotes)) 3 (https://stackoverflow.com/questions/50887431/how-to-format-docker-ps-output-to-not-use-relative-times?rq=3) How to format docker ps output to not use relative times? (https://stackoverflow.com/questions/51088771/correlating-docker-ps-with-hosts-ps?rq=3) (Question score (upvotes - downvotes)) 0 (https://stackoverflow.com/questions/51088771/correlating-docker-ps-with-hosts-ps?rq=3) Correlating `docker ps` with host's `ps` (https://stackoverflow.com/questions/53668614/sort-output-from-docker-ps-in-powershell-while-keeping-table-header-at-the-top?rq=3) (Question score (upvotes - downvotes)) 3 (https://stackoverflow.com/questions/53668614/sort-output-from-docker-ps-in-powershell-while-keeping-table-header-at-the-top?rq=3) Sort output from `docker ps` in powershell while keeping table header at the top (https://stackoverflow.com/questions/55454155/docker-ps-a-sort-by-date?rq=3) (Question score (upvotes - downvotes)) 3 (https://stackoverflow.com/questions/55454155/docker-ps-a-sort-by-date?rq=3) Docker ps -a sort by date (https://stackoverflow.com/questions/57097947/order-of-containers-in-docker-ps?rq=3) (Question score (upvotes - downvotes)) 1 (https://stackoverflow.com/questions/57097947/order-of-containers-in-docker-ps?rq=3) Order of containers in docker ps (https://stackoverflow.com/questions/60055287/docker-ps-list-only-names-and-status?rq=3) (Question score (upvotes - downvotes)) 3 (https://stackoverflow.com/questions/60055287/docker-ps-list-only-names-and-status?rq=3) docker ps list only Names and Status (https://stackoverflow.com/questions/69546846/how-to-format-docker-ps-command-into-a-list-of-dictionaries?rq=3) (Question score (upvotes - downvotes)) 1 (https://stackoverflow.com/questions/69546846/how-to-format-docker-ps-command-into-a-list-of-dictionaries?rq=3) How to format `docker ps` command into a list of dictionaries (https://stackoverflow.com/questions/73467641/docker-ps-commands-filter-with-not-condition?rq=3) (Question score (upvotes - downvotes)) 5 (https://stackoverflow.com/questions/73467641/docker-ps-commands-filter-with-not-condition?rq=3) docker ps command's filter with NOT condition (https://stackexchange.com/questions?tab=hot) Hot Network Questions (Space Exploration Stack Exchange) (https://space.stackexchange.com/questions/70050/what-is-that-extra-exhaust-on-the-rs-68) What is that extra exhaust on the RS-68 (Database Administrators Stack Exchange) (https://dba.stackexchange.com/questions/348093/calling-sp-getapplock-and-sp-releaseapplock-in-context-of-a-linked-server) Calling sp_getapplock and sp_releaseapplock in context of a linked server (Astronomy Stack Exchange) (https://astronomy.stackexchange.com/questions/61835/red-shift-puzzle) Red Shift Puzzle (MathOverflow) (https://mathoverflow.net/questions/502079/counting-non-intersecting-paths-on-the-m%c3%b6bius-strip) Counting non-intersecting paths on the Möbius strip (Electrical Engineering Stack Exchange) (https://electronics.stackexchange.com/questions/757580/why-is-amplifier-bandwidth-defined-at-%e2%88%923-db) Why is amplifier bandwidth defined at −3 dB? (Mathematics Stack Exchange) (https://math.stackexchange.com/questions/5103602/solving-system-of-equations-with-one-affine-and-two-cubic-equations) Solving system of equations with one affine and two cubic equations (Travel Stack Exchange) (https://travel.stackexchange.com/questions/200481/do-eu-citizens-need-eta-to-visit-gibraltar) Do EU citizens need ETA to visit Gibraltar? (English Language Learners Stack Exchange) (https://ell.stackexchange.com/questions/369955/how-to-divide-heterogeneous-with-hyphen) How to divide "heterogeneous" with hyphen (Worldbuilding Stack Exchange) (https://worldbuilding.stackexchange.com/questions/269531/feasibility-of-an-industrial-society-where-a-modern-tax-system-is-less-regarde) Feasibility of an industrial society where a 'modern' tax system is less regarded/effective than 'tax farming'? (Ask Ubuntu) (https://askubuntu.com/questions/1557816/xubuntu-linux-25-10-not-available-automatically) Xubuntu Linux 25.10 - not available automatically (Biblical Hermeneutics Stack Exchange) (https://hermeneutics.stackexchange.com/questions/109137/what-are-the-requirements-for-gods-word-such-that-it-will-not-return-void) What are the requirements for God's word such that it will "not return void"? (Aviation Stack Exchange) (https://aviation.stackexchange.com/questions/111526/are-737-pilots-able-to-let-passengers-hear-atc-communications) Are 737 pilots able to let passengers hear ATC communications? (Home Improvement Stack Exchange) (https://diy.stackexchange.com/questions/323746/is-reverse-polarity-in-a-lamp-a-fire-risk) Is reverse polarity in a lamp a fire risk? (Information Security Stack Exchange) (https://security.stackexchange.com/questions/283273/remote-passkey-login) Remote Passkey Login (MathOverflow) (https://mathoverflow.net/questions/501983/can-every-sidon-set-of-size-3-be-extended-to-a-finite-perfect-difference-set) Can every Sidon set of size 3 be extended to a finite perfect difference set? (Physics Stack Exchange) (https://physics.stackexchange.com/questions/861363/how-can-decompressing-neutronium-accelerate-beta-decay-to-form-heavy-elements) How can decompressing neutronium accelerate beta decay to form heavy elements? (Puzzling Stack Exchange) (https://puzzling.stackexchange.com/questions/133675/is-1-111-111-111-111-111-111-prime) Is 1,111,111,111,111,111,111 prime? (Aviation Stack Exchange) (https://aviation.stackexchange.com/questions/111530/why-is-n2-higher-than-n3-during-engine-start-on-an-a330neo) Why is N2 higher than N3 during engine start on an A330neo? (Electrical Engineering Stack Exchange) (https://electronics.stackexchange.com/questions/757571/would-this-coating-suit-for-isolating-the-copper-side-of-a-stripboard-from-outsi) Would this coating suit for isolating the copper side of a stripboard from outside world? (Worldbuilding Stack Exchange) (https://worldbuilding.stackexchange.com/questions/269506/a-replacement-for-alcohol-in-bars) A replacement for alcohol in bars (Home Improvement Stack Exchange) (https://diy.stackexchange.com/questions/323724/is-it-safe-to-dig-next-to-foundation-resting-on-soil-bedrock) Is it safe to dig next to foundation resting on soil/bedrock (Unix & Linux Stack Exchange) (https://unix.stackexchange.com/questions/800707/sort-output-using-key-and-field-separator) Sort output using "--key" and "--field-separator" (Home Improvement Stack Exchange) (https://diy.stackexchange.com/questions/323735/whats-the-purpose-of-this-panel-on-the-exterior-of-my-home) What's the purpose of this panel on the exterior of my home? (Puzzling Stack Exchange) (https://puzzling.stackexchange.com/questions/133688/finding-the-link) Finding the link () Why are you flagging this comment? (45) Probable spam. This comment promotes a product, service or website while (https://stackoverflow.com/help/promotion) failing to disclose the author's affiliation . (20) Unfriendly or contains harassment/bigotry/abuse. This comment is unkind, insulting or attacks another person or group. Learn more in our (https://stackoverflow.com/conduct/abusive-behavior) Abusive behavior policy . (39) Not needed. This comment is not relevant to the post. (19) Something else. A problem not listed above. Try to be as specific as possible. Flag comment Cancel You have 0 flags left today (GPP Locator) (https://stackoverflow.com/) (https://stackoverflow.com/) Stack Overflow (https://stackoverflow.com/questions) Questions (https://stackoverflow.com/help) Help (https://chat.stackoverflow.com/?tab=explore) Chat (https://stackoverflow.co/) Products (https://stackoverflow.co/teams/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=teams) Teams (https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=advertising) Advertising (https://stackoverflow.co/advertising/employer-branding/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=talent) Talent (https://stackoverflow.co/) Company (https://stackoverflow.co/) About (https://stackoverflow.co/company/press/) Press (https://stackoverflow.co/company/work-here/) Work Here (https://stackoverflow.com/legal) Legal (https://stackoverflow.com/legal/privacy-policy) Privacy Policy (https://stackoverflow.com/legal/terms-of-service/public) Terms of Service (https://stackoverflow.com/contact) Contact Us Your Privacy Choices (https://policies.stackoverflow.co/stack-overflow/cookie-policy) Cookie Policy (https://stackexchange.com/) Stack Exchange Network (https://stackexchange.com/sites#technology) Technology (https://stackexchange.com/sites#culturerecreation) Culture & recreation (https://stackexchange.com/sites#lifearts) Life & arts (https://stackexchange.com/sites#science) Science (https://stackexchange.com/sites#professional) Professional (https://stackexchange.com/sites#business) Business (https://api.stackexchange.com/) API (https://data.stackexchange.com/) Data (https://stackoverflow.blog/?blb=1) Blog (https://www.facebook.com/officialstackoverflow/) Facebook (https://twitter.com/stackoverflow) Twitter (https://linkedin.com/company/stack-overflow) LinkedIn (https://www.instagram.com/thestackoverflow) Instagram Site design / logo © 2025 Stack Exchange Inc; user contributions licensed under (https://stackoverflow.com/help/licensing) CC BY-SA . rev 2025.10.21.35661 By continuing to use this website, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our (https://policies.stackoverflow.co/stack-overflow/cookie-policy/) Cookie Policy . By exiting this window, default cookies will be accepted. To reject cookies, select an option from below. Necessary cookies only Customize settings