2

(Similar to How to get a Docker container's IP address from the host?, but slightly different since we are interested in all containers here)

I am running into an IP conflict with an internal server of ours which conflict with a Docker container. I have reconfigured /etc/docker/daemon.json, setting the default-address-pools setting to another network. But how do I now locate which of my seven Docker containers is happening to be using this particular IP network?

Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
  • this will return all ips of running container. `docker inspect $(docker ps -q) | jq .[].NetworkSettings.IPAddress` – Adiii Nov 08 '19 at 10:04
  • @Adiii Doesn't work for me, outputs a list of empty strings. Maybe dependent on what kind of networking you use? – Per Lundberg Nov 08 '19 at 10:12
  • if you are using host network container does not get IP adress. – Adiii Nov 08 '19 at 10:36
  • @Adiii Sure, but even if you _don't_ use host networking the field you mention can be blank. This is the case for me where I typically run almost all Docker containers using `docker-compose`, with a dedicated network for each folder in which I have a `docker-compose.yml` file. – Per Lundberg Nov 08 '19 at 10:51

2 Answers2

5

This shows all container names and their IP addresses:

docker inspect $(docker ps -q ) \
--format='{{ printf "%-50s" .Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}'
L.R.
  • 977
  • 6
  • 22
3

docker network ls helps you partially in this, by listing all Docker networks:

$ docker network ls
NETWORK ID          NAME                          DRIVER              SCOPE
3eb73575a250        bridge                        bridge              local
6ce0e4a8be79        centre_isp_services_default   bridge              local
abe7381d0bfd        elasticsearchhq_default       bridge              local
bb4387bdfac2        host                          host                local
cc3e69407994        metrics_default               bridge              local
55f1c6914497        none                          null                local
26f3247f27cc        postgres-112_default          bridge              local

docker network inspect bridge hints that the information is available under IPAM.Config.Subnet:

$ docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "3eb73575a2503d6758f8baed97ec478ebaed3e75df21b4269bd170edd94337de",
        "Created": "2019-11-08T11:33:49.489800945+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.31.0.0/24"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

One would have hoped that docker network ls --format '{{.Name}} {{.IPAM.Config.Subnet}}' would be able to give us this information, but regretfully, this is not the case. Only a few fields are available using that command.

The solution

So, we have to jump through a few hoops to get it done. This does the trick:

$ for e in $(docker network ls --format '{{.Name}}') ; do docker network inspect $e --format '{{ printf "%-40s" .Name}} {{.IPAM.Config}}'; done
bridge                                   [{172.31.0.0/24   map[]}]
centre_isp_services_default              [{172.24.0.0/16  172.24.0.1 map[]}]
elasticsearchhq_default                  [{172.23.0.0/16  172.23.0.1 map[]}]
host                                     []
metrics_default                          [{172.22.0.0/16  172.22.0.1 map[]}]
none                                     []
postgres-112_default                     [{192.168.0.0/20  192.168.0.1 map[]}]
Per Lundberg
  • 3,837
  • 1
  • 36
  • 46