Reputation: 83
I am trying to produce an output that looks like this using docker ps and the json format command
{"Names":"name"}
docker ps --format '{{json .Names}}'
outputs {"name"}
without the label.
docker ps --format '{{json .}}'
gives all the container info with labels, but I don't want everything.
Preferably based on all the placeholder names below, I would like to have an output like the below with the fields that I chose in the order I chose:
{"ID":"Container ID", "Image":"Image ID", "Names":"name"}
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.
Upvotes: 8
Views: 10206
Reputation: 143
as @mcuenez told as a comment. His proposal is in my opinion better that that from @Mickael B. since it is more generic and you don't have to write a very long command go get all the data.
docker ps --all --no-trunc --format='{{json .}}'
and to formant the output you could use jq
.
docker ps --all --no-trunc --format="{{json . }}" | jq --tab .
The output would be something like this
{
"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"
}
With jq
you can even create a valid json out of the single entries with a -s
flag.
[
{
"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"
}
]
Upvotes: 3
Reputation: 5215
You can do it with this format:
docker ps --format '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}"}'
It outputs:
{"ID":"ed3c992b7472", "Image": "alpine:3.9", "Names":"wizardly_buck"}
Upvotes: 13