Rootop 服务器运维与web架构

docker ps格式化输出显示指定列

目的:实现docker ps时显示指定列
有时候映射的端口多了,或者容器名字过长,docker ps时,屏幕太小会换行。通过去掉一些不看的列,让其显示为一行,方便查找。
通过 man docker ps 帮助文档可以看到–format参数可以实现格式化输出自定义列。

支持的列名如下:

--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.

table实现第一行打印列名,后面是GO语言里template语法(docker是基于go语言开发的)

[root@host ~]# docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Status}}"
CONTAINER ID        IMAGE                           NAMES               STATUS
a640a8b3c0d2        docker.io/rabbitmq:management   rabbitmq            Up 9 days
a814698e6c4d        centos                          new_guanwang        Up 2 weeks
06f722c239b6        c7j8t8                          guanwang            Exited (137) 2 weeks ago

这样就达到想要的格式效果了。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/4208.html

作者:Venus

服务器运维与性能优化

评论已关闭。