0

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

swiss_knight
  • 5,787
  • 8
  • 50
  • 92
  • The container-private IP address is basically useless: you can't connect to it from other hosts, on most platforms you can't connect to it from outside a container, and between containers Docker provides a DNS system so you don't need to look it up directly. Why are you trying to look it up? – David Maze Jan 15 '22 at 12:05
  • Well, you'e certainly right, it's more the `Gateway` address that I actually need, but it's near the container's IP in the `docker inspect` results. But then, I've got the same problem when trying to retrieve the Gateway IP. Which I do need to connect to external (local to the host machine) services such as PostgreSQL from inside a container. In addition,I also want to learn more on how network actually works in docker (and in general) because I lack knowledge in this field. So I wanted to make sure that the container's IP is kind of "mapped" to 'localhost' when trying to connect from the host – swiss_knight Jan 15 '22 at 12:36
  • On native Linux, if you run a container with `--add-host=host.docker.internal:host-gateway` then you can use `host.docker.internal` to reach out to the host system (on non-Linux Docker Desktop setups this is provided without special setup). See [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach). – David Maze Jan 15 '22 at 12:41
  • Yes, indeed, this is working, but then I am not able get the same behavior using services defined in a Compose file, and adding `network_mode: host` which was, at some time, recommended is actually messing up other network related stuff. It's especially no more compatible with ports binding with newest docker versions. In addition, this is also related: https://stackoverflow.com/q/70225173/6630397 (hence the idea to understand how the network is working, because sometime I can see docker creating networks from Compose files outside the standard 172.17.0.0/16 network, e.g. 172.30.x.x. – swiss_knight Jan 15 '22 at 12:49
  • Compose has a similar [`extra_hosts:`](https://docs.docker.com/compose/compose-file/compose-file-v3/#extra_hosts) setting and you can put the `host.docker.internal` definition there. Each Compose file creates a new Docker network which will (by default) allocate a new 172.xx.0.0/16 network (and a new 172.xx.0.1 gateway IP), so with the approach you're proposing you need to go through multiple steps to create the network, find its gateway address, reconfigure your Compose setup, and actually deploy. – David Maze Jan 15 '22 at 14:02

0 Answers0