Docker --格式为 json。多个占位符的特定占位符语法

Joh*_*ker 6 json ps docker

我正在尝试使用 docker ps 和 json format 命令生成如下所示的输出

{"Names":"name"}

docker ps --format '{{json .Names}}'
Run Code Online (Sandbox Code Playgroud)

输出{"name"}没有标签。

docker ps --format '{{json .}}'提供带有标签的所有容器信息,但我不想要所有内容。

最好基于下面的所有占位符名称,我希望得到如下所示的输出,其中包含按我选择的顺序选择的字段:

{"ID":"Container ID", "Image":"Image ID", "Names":"name"}
Run Code Online (Sandbox Code Playgroud)
Placeholder Description
.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.
.Networks   Names of the networks attached to this container.
Run Code Online (Sandbox Code Playgroud)

Mic*_* B. 11

您可以使用以下格式来完成:

docker ps --format '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}"}'
Run Code Online (Sandbox Code Playgroud)

它输出:

{"ID":"ed3c992b7472", "Image": "alpine:3.9", "Names":"wizardly_buck"}
Run Code Online (Sandbox Code Playgroud)

  • 您还可以使用“docker ps --all --no-trunc --format='{{json .}}'”,然后使用“|”对其进行格式化。jq.`. 最终命令看起来像这样 `docker ps --all --no-trunc --format='{{json .}}' | jq.`. (6认同)