Docker inspect formatting not working

Hey there folks.

So I was trying to use this formatting example located in the docs:

docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' $INSTANCE_ID

And it threw this into my face:

Template parsing error: template: :1:59: executing "" at <index $conf 0>: error calling index: index of untyped nil

I have no idea what the problem is. :slight_smile: Could anyone please enlighten me?

Thank you!

Hi,

I think the issue is that the container on which you try the example has not port binding on the host.
If you do a simple inspect on the container, do you have a list of Ports with HostPort like this:

"NetworkSettings": {
            ...
            "Ports": {
                "50000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "50000"
                    }
                ],
                "8080/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ]
            },

If you don’t have that but just something like that:

"NetworkSettings": {
            ...
            "Ports": {
                "8080/tcp": null
            },

then you can’t iterate on a field (.HostPort) that does not exist.

Ahhhh! Thanks @kar1! That makes perfect sense. :slight_smile:

Indeed the container did not have bindings.