4

(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?

4
  • this will return all ips of running container. docker inspect $(docker ps -q) | jq .[].NetworkSettings.IPAddress Commented Nov 8, 2019 at 10:04
  • @Adiii Doesn't work for me, outputs a list of empty strings. Maybe dependent on what kind of networking you use? Commented Nov 8, 2019 at 10:12
  • if you are using host network container does not get IP adress. Commented Nov 8, 2019 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. Commented Nov 8, 2019 at 10:51

3 Answers 3

12

This shows all container names and their IP addresses:

docker inspect $(docker ps -q ) \ --format='{{ printf "%-50s" .Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}' 
Sign up to request clarification or add additional context in comments.

1 Comment

you are great!!!! Thank you!
4

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[]}] 

1 Comment

the first command does job : docker network inspect bridge . The other command did not work.
3

Get IP addresses, with network names, for multiple networks, for all containers

A container might have several IP addresses in several networks. To save time by minimizing guesswork and troubleshooting, this extended answer is provided.

Tested on Ubuntu, for containers created with docker compose.

Query

docker inspect -f '{{printf "====================\n"}}{{.Name}} {{range $net,$v := .NetworkSettings.Networks}}{{printf "\n"}}{{printf "\n"}}{{printf "%s\n" $net}}{{.IPAddress}}{{printf "\n"}}{{end}}' $(docker ps -q)

Options for the last part of the query:

...running containers: $(docker ps -q)

...all containers (add a): $(docker ps -aq)

Result, real example

==================== /live-staging-proxy 000-traefik-proxy-extrn 172.20.0.4 live-staging_fcgi-fpm 192.168.224.3 ==================== /common-web-traefik 000-traefik-proxy-extrn 172.20.0.3 010-traefik-and-socket-proxy-internal 172.18.0.3 ==================== /common-web-socket-proxy 010-traefik-and-socket-proxy-internal 172.18.0.2 ==================== /live-staging-web live-staging_db 192.168.208.3 live-staging_fcgi-fpm 192.168.224.2 ==================== /live-staging-db live-staging_db 192.168.208.2 ==================== /c-registry-standard 000-traefik-proxy-extrn 172.20.0.2 

Result output format explained:

==================== /container-one-name network-one-name 192.168.16.2 network-two-name 192.168.96.2 ==================== /container-two-name network-two-name 192.168.96.3 network-three-name 192.168.76.2 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.