In docker config.json, how to get the IPAddress of the containers in the custom psFormat?

Published

According to the documentation at: https://docs.docker.com/engine/reference/commandline/cli/#customize-the-default-output-format-for-commands

I want to customize the docker ps output so that it shows the IP of the containers in the table results.

What I’ve tried so far is:

$ cat ~/.docker/config.json
{
  "psFormat": "table {{.ID}}t{{.Image}}t{{.IPAddress}}t{{.Ports}}t{{.Names}}"
}

but then it raises this error:

$  docker ps
Template parsing error: template: :1:33: executing "" at <.IPAddress>: 
  can't evaluate field IPAddress in type *formatter.ContainerContext

I also know that docker inspect accept a --format argument having kind of the same structure:

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{.Name}}' my-project_app_1
172.19.0.2 /my-project_app_1

So I also naively tried to copy/paste that format structure into the docker config.json file:

$ cat ~/.docker/config.json
{
  "psFormat": "table {{.ID}}t{{.Image}}t{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}t{{.Ports}}t{{.Names}}"
}

but then this error shows up:

Template parsing error: template: :1:55: executing "" at <.NetworkSettings.Networks>:
  can't evaluate field NetworkSettings in type *formatter.ContainerContext

Question

How would you get the IP of the container in the table formatted output of a custom docker ps command?

System info:
Ubuntu: 18.04.6 LTS
Kernel: 5.4.0-94-generic x86_64 GNU/Linux
Docker: Docker version 20.10.12, build e91ed57

Source: Docker Questions

Published
Categorised as docker Tagged

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq