我尝试使用docker ps和json format命令生成如下所示的输出
{"Names":"name"} docker ps --format '{{json .Names}}'
输出不带标签的{"name"}。docker ps --format '{{json .}}'提供了带有标签的所有容器信息,但我不需要所有信息。最好基于下面的所有占位符名称,我希望有一个输出,如下图所示,其中的字段是按照我选择的顺序选择的:x一个一个一个一个x一个一个二个x
{"name"}
docker ps --format '{{json .}}'
w9apscun1#
您可以使用以下格式执行此操作:
docker ps --format '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}"}'
它输出:
{"ID":"ed3c992b7472", "Image": "alpine:3.9", "Names":"wizardly_buck"}
8yoxcaq72#
正如@mcuenez在评论中所说的那样,在我看来,他的建议比@Mickael B的建议更好,因为它更通用,而且你不必写一个很长的命令来获取所有数据。
docker ps --all --no-trunc --format='{{json .}}'
为了使输出共振,你可以使用jq。
jq
docker ps --all --no-trunc --format="{{json . }}" | jq --tab .
输出将如下所示
{ "Command": "zsh", "CreatedAt": "2023-02-22 23:40:37 +0100 CET", "ID": "b2163d5d7229", "Image": "local-image-with-zsh", "Labels": "org.opencontainers.image.ref.name=ubuntu,org.opencontainers.image.version=22.04", "LocalVolumes": "0", "Mounts": "", "Names": "nervous_einstein", "Networks": "bridge", "Ports": "", "RunningFor": "14 minutes ago", "Size": "215kB (virtual 1.22GB)", "State": "running", "Status": "Up 14 minutes" } { "Command": "bash", "CreatedAt": "2023-02-22 23:39:22 +0100 CET", "ID": "1281e43c27a4", "Image": "ghcr.io/example-user/remote-image-with-bash", "Labels": "org.opencontainers.image.ref.name=ubuntu,org.opencontainers.image.version=22.04", "LocalVolumes": "0", "Mounts": "", "Names": "hardcore_cerf", "Networks": "bridge", "Ports": "", "RunningFor": "15 minutes ago", "Size": "216kB (virtual 1.22GB)", "State": "running", "Status": "Up 15 minutes" }
使用jq,您甚至可以从带有-s标志的单个条目中创建有效的json。
-s
[ { "Command": "zsh", "CreatedAt": "2023-02-22 23:40:37 +0100 CET", "ID": "b2163d5d7229", "Image": "local-image-with-zsh", "Labels": "org.opencontainers.image.ref.name=ubuntu,org.opencontainers.image.version=22.04", "LocalVolumes": "0", "Mounts": "", "Names": "nervous_einstein", "Networks": "bridge", "Ports": "", "RunningFor": "20 minutes ago", "Size": "215kB (virtual 1.22GB)", "State": "running", "Status": "Up 20 minutes" }, { "Command": "bash", "CreatedAt": "2023-02-22 23:39:22 +0100 CET", "ID": "1281e43c27a4", "Image": "ghcr.io/example-user/remote-image-with-bash", "Labels": "org.opencontainers.image.ref.name=ubuntu,org.opencontainers.image.version=22.04", "LocalVolumes": "0", "Mounts": "", "Names": "hardcore_cerf", "Networks": "bridge", "Ports": "", "RunningFor": "21 minutes ago", "Size": "216kB (virtual 1.22GB)", "State": "running", "Status": "Up 21 minutes" } ]
2条答案
按热度按时间w9apscun1#
您可以使用以下格式执行此操作:
它输出:
8yoxcaq72#
正如@mcuenez在评论中所说的那样,在我看来,他的建议比@Mickael B的建议更好,因为它更通用,而且你不必写一个很长的命令来获取所有数据。
为了使输出共振,你可以使用
jq
。输出将如下所示
使用
jq
,您甚至可以从带有-s
标志的单个条目中创建有效的json。