前言
如果经常和 Docker 打交道,最常用的三个命令一定是:
Bashdocker images
docker ps
docker stats
默认的表格输出虽然信息全面,但列太多、太宽,经常会导致终端换行,看起来非常乱。尤其是容器名字很长、端口映射一堆的时候,眼睛都看花了。
幸好 Docker 从很早就支持通过 ~/.docker/config.json 来自定义这些命令的默认输出格式,而且用的是 Go Template[1],极其强大。
配置
为了解决上述痛点,定制了 Docker 输出格式,配置如下:
JSON{
"imagesFormat": "table {{.Repository}}\\t{{.Tag}}\\t{{.ID}}\\t{{.CreatedSince}}\\t{{.Size}}",
"psFormat": "table {{.Names}}\\t{{.Image}}\\t{{.RunningFor}}\\t{{.Status}}\\t{{.Ports}}",
"statsFormat": "table {{.Name}}\\t{{.CPUPerc}}\\t{{.MemPerc}}\\t{{.MemUsage}}\\t{{.NetIO}}\\t{{.BlockIO}}"
}
没有该文件就创建一个,保存后无需重启 Docker,下一次执行命令就立刻生效。
进阶
想在 ps 中显示标签?加 {{.Labels}}
想截断超长名字?用 {{printf \"%.30s\" .Names}}
想显示容器网络 IP?用 {{range $net, $ip := .NetworkSettings.Networks}}{{$net}}:{{$ip}} {{end}}

