我在使用时没有找到任何方式来订购我的结果 docker ps
在我的情况下,我想通过.Ports订购
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"
Run Code Online (Sandbox Code Playgroud)
我如何订购结果?
小智 8
如果仅按输出列排序就足够了,您可以使用以下命令:
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" | (read -r; printf "%s\n" "$REPLY"; sort -k 3 )
Run Code Online (Sandbox Code Playgroud)
我还添加了一个代码,用于跳过表头并仅排序ps输出数据.
列出容器
docker ps
Run Code Online (Sandbox Code Playgroud)
格式
docker ps -a --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}" | (read -r; printf "%s\n" "$REPLY"; sort -k 3 )
Run Code Online (Sandbox Code Playgroud)
概要
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.
Run Code Online (Sandbox Code Playgroud)
显示容器及其命令
docker ps --format "{{.ID}}: {{.Command}}"
Run Code Online (Sandbox Code Playgroud)
在表格中显示带有标签的容器
docker ps --format "table {{.ID}}\t{{.Labels}}"
Run Code Online (Sandbox Code Playgroud)
在表中显示带有节点标签的容器
docker ps --format 'table {{.ID}}\t{{(.Label "com.docker.swarm.node")}}'
Run Code Online (Sandbox Code Playgroud)
我构建了一个docker ps pretty print函数,可以将其放入您的文件.bash_profile或.bashrc文件中,该文件的工作方式有点像别名docker ps(带有颜色输出)。 @ art-rock-guitar-superhero建议显示了如何排序,但是我已经包含了这个答案,因为每次键入--format选项和管道sort会有点乏味。
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
}
Run Code Online (Sandbox Code Playgroud)
用法:$ docker ps -p。
编辑: 我从@BrianVosburgh的评论中添加了建议。另外,我一直忘记键入,-p因此我将标志切换为此功能-a,这是我的常规用法docker ps。
| 归档时间: |
|
| 查看次数: |
5654 次 |
| 最近记录: |