如何在Linux终端便捷查看并按创建日期排序Docker卷
I feel your pain—sifting through those cryptically named Docker volumes to find the most recently created ones without a GUI (especially on Debian where Windows Docker GUI and VS Code's Docker extension don't help) is such a hassle. Instead of manually running docker volume inspect on every single volume, here are a few terminal-friendly methods to sort volumes by their creation date:
Basic Sorted List (Name + Creation Date)
First, make sure you have jq installed (it's a lightweight tool for parsing JSON, which Docker uses for inspect output). If you don't have it yet, install it on Debian with:
sudo apt update && sudo apt install jq
Then run this command to get a list of all volumes, sorted from newest to oldest:
docker volume inspect $(docker volume ls -q) | jq -r '.[].Name + " | " + .[].CreatedAt' | sort -k2,2r
Let me break down what each part does:
docker volume ls -q: Outputs only the names of all your Docker volumes (no extra fluff)docker volume inspect ...: Fetches detailed JSON data for each volumejq -r '.[].Name + " | " + .[].CreatedAt': Extracts just the volume name and creation timestamp, formatting them as a readable stringsort -k2,2r: Sorts the results by the second column (the creation date) in reverse order (newest first)
Clean Tabular Format
For a more organized, table-like view that's easier to scan, use this command instead:
docker volume inspect $(docker volume ls -q) | jq -r '.[].Name + "\t" + .[].CreatedAt' | sort -k2,2r | column -t
The column -t command aligns all the columns neatly, so you won't have messy spacing between names and dates.
Filter to Show Only the Newest Volumes
If you only care about the most recent N volumes (say, the last 5), add head -N to the end of the command:
docker volume inspect $(docker volume ls -q) | jq -r '.[].Name + "\t" + .[].CreatedAt' | sort -k2,2r | column -t | head -5
Note:
If you're running an older version of Docker where the CreatedAt field doesn't exist (unlikely in modern versions), this method won't work. But for most Debian setups with recent Docker versions, this should solve your problem perfectly.
内容的提问来源于stack exchange,提问作者bilal.h

