cli icon indicating copy to clipboard operation
cli copied to clipboard

Add .IPAddresses as formatting option on docker ps

Open thaJeztah opened this issue 3 years ago • 4 comments

fixes https://github.com/moby/moby/issues/8786

This allows showing the IP address for each network that the container is attached to, for example:

docker network create foo
docker run -d --name foo nginx:alpine
docker network connect foo foo

docker container ls --format 'table {{.ID}}\\t{{join .IPAddresses ", "}}'
CONTAINER ID   IP ADDRESSES
17e7d1910fc0   bridge:172.17.0.2, foo:172.19.0.2

docker container ls --format='{{json .IPAddresses}}' | jq .
[
  "bridge:172.17.0.2",
  "foo:172.19.0.2"
]

- Description for the changelog

thaJeztah avatar Sep 21 '20 14:09 thaJeztah

Had this branch on my computer; not sure if we should merge (see discussion on https://github.com/moby/moby/issues/8786), but thought to at least push it to github 😂

thaJeztah avatar Sep 21 '20 14:09 thaJeztah

Are there any blockers (unrelated to Moby) to get this PR merged into Docker CLI? I think this would make the --format option for docker ps a lot more powerful.

codecat avatar May 10 '22 12:05 codecat

I want to have another look at this (also taking into account IPv6 addresses); slightly wondering if the <network-name>:<ip address> is the right / most usable format.

I don't understand the need for ps command uses an intermediate ContainerContext type and not just expose the plain types.Container returned by the API for formatting

I think this was done to decorate the struct with formatting functions (such as this one); the formatting is meant for "printing on the CLI", and is kinda separate from both "client" and "api", so may not be very suitable for inclusion in the upstream moby repository (as part of the types)

thaJeztah avatar May 11 '22 10:05 thaJeztah

right, I've been confused by the naming used on ContainerContext. Would have used ContainerDecorator or something like this, Context doesn't make any sense to me here.

ndeloof avatar May 11 '22 13:05 ndeloof