Docker格式化命令输出
source link: https://chegva.com/5716.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Docker 使用Go 模板,您可以使用它来操作某些命令和日志驱动程序的输出格式。
Docker 提供了一组基本函数来操作模板元素。所有这些示例都使用该docker inspect命令,但许多其他 CLI 命令都有一个--format标志,并且许多 CLI 命令参考包括自定义输出格式的示例。
使用该--format标志时,您需要观察您的 shell 环境。在 Posix shell 中,您可以使用单引号运行以下命令:
docker inspect --format '{{join .Args " , "}}'
否则,在 Windows shell(例如 PowerShell)中,您需要使用单引号,但在 params 中转义双引号,如下所示:
docker inspect --format '{{join .Args \" , \"}}'
格式化选项(--format)
.ID 容器ID
.Image 镜像ID
.Command Quoted command
.CreatedAt 创建容器的时间点.
.RunningFor 从容器创建到现在过去的时间.
.Ports 暴露的端口.
.Status 容器状态.
.Size 容器占用硬盘大小.
.Names 容器名称.
.Labels 容器所有的标签.
.Label 指定label的值 例如'{{.Label “com.docker.swarm.cpu”}}’
.Mounts 挂载到这个容器的数据卷名称
join
join连接字符串列表以创建单个字符串。它在列表中的每个元素之间放置一个分隔符。
docker inspect --format '{{join .Args " , "}}' container
table
table 指定要查看其输出的字段。
docker image list --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}"
JSON
json 将元素编码为 json 字符串。
docker inspect --format '{{json .Mounts}}' container
lower
lower 将字符串转换为其小写表示。
docker inspect --format "{{lower .Name}}" container
split
split 将字符串切片为由分隔符分隔的字符串列表。
docker inspect --format '{{split .Image ":"}}'
title
title 将字符串的第一个字符大写。
docker inspect --format "{{title .Name}}" container
upper
upper 将字符串转换为其大写表示。
docker inspect --format "{{upper .Name}}" container
println
println 在新行上打印每个值。
docker inspect --format='{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}' container
要找出可以打印哪些数据,请将所有内容显示为 json:
docker container ls --format='{{json .}}'
也可以显示为table:
docker ps --format "table{{.Names}}\t{{.Status}}\t{{.Image}}"
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK