Ben*_*jam 5 shell go go-templates docker
我想生成一个合成docker ps表,但我的 docker 映像路径很长。我尝试做的解决方案是仅显示图像名称的最后一部分:
这就是我所拥有的:
\ndocker ps --format "table {{.Names}}\\\\t{{.Image}}\\\\t{{.Status}}\\\\t{{.Command}}"\n\nNAMES IMAGE STATUS COMMAND\na_container a_local_image Up 36 minutes "python\xe2\x80\xa6"\nanother_container registry.example.com/group/subgroup/project/my_image:latest Up 38 minutes "go\xe2\x80\xa6"\nRun Code Online (Sandbox Code Playgroud)\n我想:
\ndocker ps --format "table {{.Names}}\\\\t{{ <magic .Image> }}\\\\t{{.Status}}\\\\t{{.Command}}"\n\nNAMES IMAGE STATUS COMMAND\na_container a_local_image Up 36 minutes "python\xe2\x80\xa6"\nanother_container my_image:latest Up 38 minutes "go\xe2\x80\xa6"\nRun Code Online (Sandbox Code Playgroud)\n所以基本上,得到最后一个之后的内容/,就像basename那样。
这是我尝试过的:
\n# No fixed length\ndocker ps --format "table {{ slice .Image 0 10}}"\n\n#\xc2\xa0No last index available\ndocker ps --format 'table {{ (split "/" .Image) }}'\nRun Code Online (Sandbox Code Playgroud)\n任何帮助或解决方法表示赞赏。
\nps:由于我使用 docker cli,我认为我无法定义在模板表达式中使用的自定义函数。
\n您可以使用下一个解决方法,我将它放在 shell 文件中只是因为它太长,但是您可以直接在 shell 中调用它。
测试文件
paste <(docker ps --format 'table {{.ID}}') \
<(docker ps --format 'table {{.Image}}' | awk -F '/' '{print $NF}') \
<(docker ps --format 'table {{.Command}} {{.CreatedAt}} {{.Status}} {{.Ports}} {{.Names}}')
Run Code Online (Sandbox Code Playgroud)
解释:
docker ps --format 'table {{.ID}}'得到ID列docker psdocker ps --format 'table {{.Image}}' | awk -F '/' '{print $NF}'),首先获取Image列,然后使用awksplit /,根据需要获取最后一个子列。docker pspaste命令组合上述命令的输出。| 归档时间: |
|
| 查看次数: |
560 次 |
| 最近记录: |