Skip to content

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Sep 21, 2020

fixes moby/moby#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
Copy link
Member Author

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

@thaJeztah thaJeztah force-pushed the ps_ip_address_details branch from d5ebad2 to 72a56bd Compare September 22, 2020 14:03
@thaJeztah thaJeztah force-pushed the ps_ip_address_details branch from 72a56bd to d24907b Compare March 30, 2022 19:25
@codecat
Copy link

codecat commented May 10, 2022

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.

Copy link
Contributor

@ndeloof ndeloof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 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

@thaJeztah
Copy link
Member Author

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)

@ndeloof
Copy link
Contributor

ndeloof commented May 11, 2022

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.

@thaJeztah thaJeztah force-pushed the ps_ip_address_details branch from d24907b to 654ba31 Compare August 24, 2025 15:10
@thaJeztah thaJeztah requested a review from a team as a code owner August 24, 2025 15:10
@thaJeztah thaJeztah force-pushed the ps_ip_address_details branch from 654ba31 to 63e3c5a Compare August 24, 2025 15:11
@codecov-commenter
Copy link

codecov-commenter commented Aug 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@thaJeztah thaJeztah force-pushed the ps_ip_address_details branch from 63e3c5a to f43ec0e Compare August 24, 2025 16:47
Add a custom join function that allows for non-string slices to be
joined, following the same rules as "fmt.Sprint", it will use the
fmt.Stringer interface if implemented, or "error" if the type has
an "Error()".

For maps, it joins the map-values, for example:

    docker image inspect --format '{{join .Config.Labels ", "}}' ubuntu
    24.04, ubuntu

Signed-off-by: Sebastiaan van Stijn <[email protected]>
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

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

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

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Still failing when joining;

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

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

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the ps_ip_address_details branch from f43ec0e to 0e8f85c Compare August 24, 2025 17:26
@thaJeztah
Copy link
Member Author

This still needs a bit of work, and I'll probably split the first commit, but was rebasing old PRs, and trying to make it work and a bit more flexible than just a string ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: show container IP address with docker ps command
7 participants