Skip to content

Instantly share code, notes, and snippets.

@sofianinho
Last active November 23, 2016 08:30
Show Gist options
  • Save sofianinho/46e3939604295821572c73ca2e2e82b5 to your computer and use it in GitHub Desktop.
Save sofianinho/46e3939604295821572c73ca2e2e82b5 to your computer and use it in GitHub Desktop.
Find number of addresses already attributed by docker engine
#!/bin/sh
# part by part:
# 1. "brctl showmacs docker0" show the MAC addresses attached to the docker0 bridge
# 2. "$(($(brctl showmacs docker0|wc -l) - 1))" returns the number of the lines returned by the brctl command minus 1 (to exclude the header, for cleanup)
# 3. "uniq" show only unique enteries
# 4. "wc -l" count the number of lines in the result. Which is also the number of running containers with an IP address behind the docker0 bridge.
# Next address given by the docker engine is: (@IP of docker0)+the found number+1
brctl showmacs docker0|tail -n $(($(brctl showmacs docker0|wc -l) - 1))|uniq|wc -l
#Alternative through docker
#1. the docker inspect command with its arguments returns allocated IP addresses
#2. the second part count the addresses returned by forst part
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)|wc -l
# PS. If a container is given multiple addresses, on the same subnet or different subnets... you have to adapt these commands (if necessary)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment