Search code examples
jsondockerps

Docker --format with json. Specific placeholder syntax for multiple placeholders


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.

Solution

  • 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"}