-
-
Save ipedrazas/2c93f6e74737d1f8a791 to your computer and use it in GitHub Desktop.
function drips(){ | |
docker ps -q | xargs -n 1 docker inspect --format '{{ .NetworkSettings.IPAddress }} {{ .Name }}' | sed 's/ \// /' | |
} |
docker ps -q | xargs -n 1 docker inspect --format '{{ .NetworkSettings.Networks.dockernet.IPAddress }} {{ .Name }}' | sed 's/ // /'
A dip
extended command listing container working dir + container image:
function dip() {
_print_container_info() {
local container_id
local container_ports
local container_ip
local container_name
local container_image
container_id="${1}"
container_ports=( $(sudo docker port "$container_id" | grep -o "0.0.0.0:.*" | cut -f2 -d:) )
container_name="$(sudo docker inspect --format "{{ .Name }}" "$container_id" | sed 's/\///')"
container_workdir="$(sudo docker inspect --format '{{index .Config.Labels "com.docker.compose.project.working_dir" }}' "$container_id")"
container_ip="$(sudo docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" "$container_id")"
container_image="$(sudo docker inspect --format "{{ .Config.Image }}" "$container_id" | sed 's/\///')"
printf "%-40s %-50s %-20s %-20s %-72s\n" "$container_workdir" "$container_name" "$container_ip" "${container_ports[*]}" "$container_image"
}
local container_id
container_id="$1"
printf "%-40s %-50s %-20s %-20s %-72s\n" 'Container directory' 'Container Name' 'Container IP' 'Container Ports' 'Container Image'
if [ -z "$container_id" ]; then
local container_id
sudo sudo docker ps -a --format "{{.Names}} {{.ID}}" | sort | sed 's#^[^ ]\+ ##g' | while read -r container_id ; do
_print_container_info "$container_id"
done
else
_print_container_info "$container_id"
fi
}
output example:
Container directory Container Name Container IP Container Ports Container Image
/opt/pod/abes-filebeat-docker abes-filebeat 192.168.192.2 docker.elastic.cobeats/filebeat:7.17.0
/opt/pod/abes-hello-docker abes-hello-batch 172.30.0.2 abesesrabes-hello:develop-batch
/opt/pod/abes-hello-docker abes-hello-front 172.30.0.3 7080 abesesrabes-hello:develop-front
/opt/pod/abes-hello-docker abes-hello-web 172.30.0.4 7081 abesesrabes-hello:develop-web
/opt/pod/microwebservices-docker bacon-cache-warmer-kbart-withdate 192.168.48.6 abesesrbacon-cache-warmer:1.3.1
/opt/pod/microwebservices-docker bacon-cache-warmer-kbart-withoutdate 192.168.48.5 abesesrbacon-cache-warmer:1.3.1
The solutions I see here spawn unnecessarily many processes. (Well, necessary for readable code but not absolutely necessary for the functionality.) I've developed this piece of abomination:
dip () {
docker_ps="$(
docker ps --format 'table{{.Names}}|{{.Status}}|{{.Size}}' |
tail --lines +2 |
sort
)"
# https://stackoverflow.com/a/20686101
FORMAT='{{.Name}}|{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
ip_addresses=`
echo "$docker_ps" |
cut --fields 1 --delimiter '|' |
while read -r container; do
docker inspect --format=$FORMAT $container
done |
sed 's|^/||' |
sort
`
{
FM="\033[1m%s\033[0m|\033[1m%s\033[0m|\033[1m%s\033[0m|\033[1m%s\033[0m"
printf "$FM\n" NAMES IP STATUS SIZE
join -t "|" <(echo "$ip_addresses") <(echo "$docker_ps")
} |
column --separator "|" --output-separator "┊" --table
}
Sample output:
NAMES ┊IP ┊STATUS ┊SIZE
dnsmasq┊172.18.0.2┊Up 7 hours ┊2B (virtual 6.64MB)
esphome┊172.19.0.2┊Up 5 minutes┊422kB (virtual 270MB)
Notes:
- I cannot format it here but the header row is shown bold in the terminal.
- You'll probably have to modify this a bit it according to your favorite fields.
- I wouldn't be surprised if
docker inspect
does the job on its own with a contrived enough formatting string. But this was my very first real world use of thejoin
command. - I thought replacing
while read -r container
withxargs
would squeeze out some more speed; buttime
says otherwise. - My code also spawns too many processes, but at least just two
docker
processes. Since thedocker
commands are the expensive part, my code is superior to everything else here. Not. Itime
d the stuff here against my code, and my code is slower heh heh heh. Butdocker
may be caching results, making my measurement method faulty. Probably not. - Manually adding the header goes against the single source of truth principle: You have to modify two places in order to get one modification. But saves some spawns (and bash-induced-headaches).
Wow, I hadn't realized people were updating this! Earlier this year I moved the command into a little bash script so I could add some extra helpers like tailing logs and connecting with bash (and so I didn't have to manually sync the changes between machines, just a simple pull
): https://github.com/GammaGames/dz
I just updated it with some of your improvements 😄️
The dip
command is amazing.
One thing I would suggest is moving the "Container directory" to the end as with long directory names it completely breaks the formatting.
@a1ea321 has inspired me to update the dig function:
_print_container_info() {
local container_id
local container_ports
local container_ip
local container_gateway
local container_ipv6
local container_ipv6_gateway
local container_name
container_id="${1}"
container_ports=( $(docker port "$container_id" | grep -o "0.0.0.0:.*" | cut -f2 -d:) )
container_name="$(docker inspect --format "{{ .Name }}" "$container_id" | sed 's/\///')"
container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" "$container_id")"
container_gateway="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}" "$container_id")"
container_ipv6="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.GlobalIPv6Address}}{{end}}" "$container_id")"
container_ipv6_gateway="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPv6Gateway}}{{end}}" "$container_id")"
echo ''$container_id','$container_name','$container_ip','$container_gateway','$container_ipv6','$container_ipv6_gateway','${container_ports[*]}'' >> "~/tmp/docker-container.txt"
}
touch "~/tmp/docker-container.txt"
echo 'ID,NAME,IP,Gateway,IPv6,IPv6 Gateway,PORTS' >> "~/tmp/docker-container.txt"
local container_id
container_id="$1"
if [ -z "$container_id" ]; then
local container_id
docker ps -a --format "{{.ID}}" | while read -r container_id ; do
_print_container_info "$container_id"
done
else
_print_container_info "$container_id"
fi
column -s "," --output-separator " ┊ " -t "~/tmp/docker-container.txt"
rm -r "~/tmp/docker-container.txt"
}
This shows beautifully as:
ID ┊ NAME ┊ IP ┊ Gateway ┊ IPv6 ┊ IPv6 Gateway ┊ PORTS
221aa9e5f8a9 ┊ alpine-A ┊ 172.20.0.2 ┊ 172.20.0.1 ┊ ┊ ┊
21dfcac1a96f ┊ alpine-bridge2 ┊ 172.17.0.6 ┊ 172.17.0.1 ┊ fd00::1:0:0:2 ┊ fd00::1:0:0:1 ┊
07d37817152f ┊ alpine-bridge3 ┊ 172.17.0.3 ┊ 172.17.0.1 ┊ fd00::1:0:0:3 ┊ fd00::1:0:0:1 ┊
02315ce29e45 ┊ alpine-B ┊ 172.21.0.2 ┊ 172.21.0.1 ┊ ┊ ┊
34c3247c609b ┊ portainer ┊ 172.17.0.2 ┊ 172.17.0.1 ┊ fd00::1:242:ac11:2 ┊ fd00::1:0:0:1 ┊ 9000
It'd be nice to have the tempfile as variable, but i don't know how to do that
In case of interest for anyone:
------------------------------------------------------
NETWORK ┊ SUBNET IPv4 ┊ GATEWAY IPv4 ┊ SUBNET IPv6 ┊ GATEWAY IPv6 ┊ ID
A ┊ "172.18.0.0/16" ┊ "172.18.0.1" ┊ "fd00::a:0:0:0/80" ┊ null ┊ 76163ba9d48d
B ┊ "172.19.0.0/16" ┊ "172.19.0.1" ┊ "fd00::b:0:0:0/80" ┊ null ┊ 07d4d008ccb7
bridge ┊ "172.17.0.0/16" ┊ "172.17.0.1" ┊ "fd00::/64" ┊ null ┊ b866c9f00180
host ┊ null ┊ null ┊ null ┊ null ┊ 1bbbccd2171a
none ┊ null ┊ null ┊ null ┊ null ┊ 0016304b38be
------------------------------------------------------
CONTAINER ┊ IP ┊ Gateway ┊ Mac ┊ IPv6 ┊ IPv6 Gateway ┊ PORTS ┊ Network ┊ ID
alpine-B ┊ 172.19.0.2 ┊ 172.19.0.1 ┊ 00:00:00:00:00:01 ┊ ┊ ┊ ┊ B ┊ cd73a2ea1317
alpine-bridge3 ┊ 172.17.0.2 ┊ 172.17.0.1 ┊ 00:00:00:00:00:03 ┊ fd00::3 ┊ fd00::1 ┊ ┊ bridge ┊ 4dc53b04f25c
alpine-bridge2 ┊ 172.17.0.3 ┊ 172.17.0.1 ┊ 00:00:00:00:00:02 ┊ fd00::2 ┊ fd00::1 ┊ ┊ bridge ┊ f5230f1ab0cb
alpine-A ┊ 172.18.0.2 ┊ 172.18.0.1 ┊ 00:00:00:00:00:01 ┊ ┊ ┊ ┊ A ┊ d0befc3262d3
------------------------------------------------------
this can be achieved with following functions:
function nip() {
_print_network_info() {
local network_name
local network_gatewayIPv4
local network_subnetIPv4
local network_gatewayIPv6
local network_subnetIPv6
network_id="${1}"
network_name="$(docker inspect --format "{{ .Name }}" "$network_id" | sed 's/\///')"
network_subnetIPv4="$(docker inspect $network_id | jq '.[].IPAM.Config[0].Subnet')"
network_gatewayIPv4="$(docker inspect $network_id | jq '.[].IPAM.Config[0].Gateway')"
network_subnetIPv6="$(docker inspect $network_id | jq '.[].IPAM.Config[1].Subnet')"
network_gatewayIPv6="$(docker inspect $network_id | jq '.[].IPAM.Config[1].Gateway')"
echo ''$network_name','$network_subnetIPv4','$network_gatewayIPv4','$network_subnetIPv6','$network_gatewayIPv6','$network_id'' >> "~/tmp/docker-network.txt"
}
touch "~/tmp/docker-network.txt"
echo 'NETWORK,SUBNET IPv4,GATEWAY IPv4,SUBNET IPv6,GATEWAY IPv6,ID' >> "~/tmp/docker-network.txt"
local network_id
networkr_id="$1"
if [ -z "$network_id" ]; then
local network_id
docker network ls --format "{{.ID}}" | while read -r network_id ; do
_print_network_info "$network_id"
done
else
_print_network_info "$network_id"
fi
column -s "," --output-separator " ┊ " -t "~/tmp/docker-network.txt"
rm -r "~/tmp/docker-network.txt"
}
echo '------------------------------------------------------'
nip;
echo '------------------------------------------------------'
function cip() {
_print_container_info() {
local container_id
local container_ports
local container_ip
local container_gateway
local container_ipv6
local container_ipv6_gateway
local container_name
local container_mac
local container_network
container_id="${1}"
container_ports=( $(docker port "$container_id" | grep -o "0.0.0.0:.*" | cut -f2 -d:) )
container_name="$(docker inspect --format "{{ .Name }}" "$container_id" | sed 's/\///')"
container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" "$container_id")"
container_gateway="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.Gateway}}{{end}}" "$container_id")"
container_mac="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}" "$container_id")"
container_ipv6="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.GlobalIPv6Address}}{{end}}" "$container_id")"
container_ipv6_gateway="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPv6Gateway}}{{end}}" "$container_id")"
container_network="$(docker inspect --format "{{.HostConfig.NetworkMode}}" "$container_id" | sed 's/\///')"
echo ''$container_name','$container_ip','$container_gateway','$container_mac','$container_ipv6','$container_ipv6_gateway','${container_ports[*]}','$container_network','$container_id'' >> "~/tmp/docker-container.txt"
}
touch "~/tmp/docker-container.txt"
echo 'CONTAINER,IP,Gateway,Mac,IPv6,IPv6 Gateway,PORTS,Network,ID' >> "~/tmp/docker-container.txt"
local container_id
container_id="$1"
if [ -z "$container_id" ]; then
local container_id
docker ps -a --format "{{.ID}}" | while read -r container_id ; do
_print_container_info "$container_id"
done
else
_print_container_info "$container_id"
fi
column -s "," --output-separator " ┊ " -t "~/tmp/docker-container.txt"
rm -r "~/tmp/docker-container.txt"
}
cip;
echo '------------------------------------------------------'
[root@localhost ~]# docker ps -a|awk 'NR > 1 {print $1}' | xargs -n 1 docker inspect --format '{{ .ID }} {{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' | sed 's#^/##';
26bd5c6380e334e518a9fd967099a649a9768b99bdbd85cf4f6e78c84ace0b39 172.20.0.167
99c294b61cb7405027eff83cf3db0cb2e09aa823faaba08287a0d8967a8b9002 172.17.0.2
cf12cdf6318e7875361d5ec1fda93c2659e5444440cc08a9ef821f36d72dd86e 172.17.0.3
Adding my two cents.
- Ports
All examples so far are getting ports from 0.0.0.0, but (for those that don't know), we can listen to a port in a specific IP, not just 0.0.0.0 (or *).
This way we can have different services in the same port, but with different IPs (to access both you will probably need a reverse proxy or edit /etc/hosts or your DNS, but it is out of scope here).
docker run -d ... -p 192.168.32.12:53:53 ...
docker run -d ... -p 192.168.44.5:53:53 ...
I use it in my PiHole, and virt-manager. PiHole listens my LAN IP, virt-manager listens its own subnet...
I am using this to extract the ports from all IPs. To get IPv6, I am using a different expression.
container_ports=( $(docker port "$container_id" | grep -o -e ":[0-9]\+*" | cut -f2 -d:) )
- IPAddress and Gateway
The same container can be added to different Networks, and on each one it will get its own IP and Gateway. The commands are correct, but it is better to add a space just to visually represent better.
container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}" "$container_id")"
container_gateway="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.Gateway}} {{end}}" "$container_id")"
- Networks
Still in the case of a container in multiple networks, just.HostConfig.NetworkMode
won't give you all of them.
Loop.NetworkSettings.Networks
to getkey
andvalue
and print just thekey
.
container_network=( $(docker inspect --format "{{range \$k, \$v := .NetworkSettings.Networks}}{{printf \"%s\n\" \$k}}{{end}}" "$container_id") )
For now, I am happy with this.
Other changes:
- search for ID or Name
- only calls inner function if ID/Name found
- show only ID's first 6 chars
- inspect only network or only container
function nip() {
_print_network_info() {
local network_search
local network_name
local network_id
local network_gatewayIPv4
local network_subnetIPv4
local network_gatewayIPv6
local network_subnetIPv6
network_search="${1}"
network_name="$(docker network inspect --format "{{.Name}}" "$network_search" | sed 's/\///')"
network_id="$(docker network inspect --format "{{.ID}}" "$network_search")"
network_subnetIPv4="$(docker network inspect $network_search | jq '.[].IPAM.Config[0].Subnet')"
network_gatewayIPv4="$(docker network inspect $network_search | jq '.[].IPAM.Config[0].Gateway')"
network_subnetIPv6="$(docker network inspect $network_search | jq '.[].IPAM.Config[1].Subnet')"
network_gatewayIPv6="$(docker network inspect $network_search | jq '.[].IPAM.Config[1].Gateway')"
echo ''$network_name','$network_subnetIPv4','$network_gatewayIPv4','$network_subnetIPv6','$network_gatewayIPv6','${network_id:0:6}'' >>~/tmp/docker-network.txt
}
touch ~/tmp/docker-network.txt
echo '------------------------------------------------------'
echo 'NETWORK,SUBNET IPv4,GATEWAY IPv4,SUBNET IPv6,GATEWAY IPv6,ID' >>~/tmp/docker-network.txt
local network_search
network_search="$1"
if [ -z "$network_search" ]; then
# if $network_search is empty
docker network ls --format "{{.ID}}" | while read -r network_search; do
_print_network_info "$network_search"
done
else
# only calls _print_network_info if $network_search exits
docker network ls --format "{{.ID}} {{.Name}}" | grep -q "\b$network_search\b" && _print_network_info "$network_search"
fi
column -s "," --output-separator " ┊ " -t ~/tmp/docker-network.txt
echo '------------------------------------------------------'
rm -r ~/tmp/docker-network.txt
}
function cip() {
_print_container_info() {
local container_search
local container_id
local container_ports
local container_ip
local container_gateway
local container_ipv6
local container_ipv6_gateway
local container_name
local container_mac
local container_network
container_search="${1}"
container_ports=($(docker port "$container_search" | grep -o ":[0-9]\+" | cut -f2 -d:))
container_name="$(docker container inspect --format "{{.Name}}" "$container_search" | sed 's/\///')"
container_id="$(docker container inspect --format "{{.ID}}" "$container_search")"
container_ip="$(docker container inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}" "$container_search")"
container_gateway="$(docker container inspect --format "{{range .NetworkSettings.Networks}}{{.Gateway}} {{end}}" "$container_search")"
container_mac="$(docker container inspect --format "{{range .NetworkSettings.Networks}}{{.MacAddress}} {{end}}" "$container_search")"
container_ipv6="$(docker container inspect --format "{{range .NetworkSettings.Networks}}{{.GlobalIPv6Address}} {{end}}" "$container_search")"
container_ipv6_gateway="$(docker container inspect --format "{{range .NetworkSettings.Networks}}{{.IPv6Gateway}} {{end}}" "$container_search")"
container_network=($(docker container inspect --format "{{range \$k, \$v := .NetworkSettings.Networks}}{{printf \"%s\n\" \$k}}{{end}}" "$container_search"))
echo ''$container_name','$container_ip','$container_gateway','$container_mac','$container_ipv6','$container_ipv6_gateway','${container_ports[*]}','${container_network[*]}','${container_id:0:6}'' >>~/tmp/docker-container.txt
}
touch ~/tmp/docker-container.txt
echo '------------------------------------------------------'
echo 'CONTAINER,IP,Gateway,Mac,IPv6,IPv6 Gateway,PORTS,Networks,ID' >>~/tmp/docker-container.txt
local container_search
container_search="$1"
if [ -z "$container_search" ]; then
# if $container_search is empty
docker ps -a --format "{{.ID}}" | while read -r container_search; do
_print_container_info "$container_search"
done
else
# only calls _print_container_info if $container_search exits
docker container ls --format "{{.ID}} {{.Names}}" | grep -q "\b$container_search\b" && _print_container_info "$container_search"
fi
column -s "," --output-separator " ┊ " -t ~/tmp/docker-container.txt
echo '------------------------------------------------------'
rm -r ~/tmp/docker-container.txt
}
Anyone wanna add these changes to the repo version? https://github.com/GammaGames/dz
It’s also got a command to sh into containers.
showing local ports might be useful too, sometimes I bind stuff to 127.0.0.1
and they don’t show up
Edit: brccabral, thanks for the pr! I've also updated the ip function to show host ip when the container isn't listening on 0.0.0.0
since your changes made that way easier to implement, here's what that looks like with a bunch of different configs:
Container Ports
5432:5432/tcp
127.0.0.1:9090:80/tcp
127.0.0.1:8082:80/tcp
127.0.0.1:8084:80/tcp
127.0.0.1:9093:80/tcp
127.0.0.1:8083:80/tcp
host
127.0.0.1:9092:80/tcp
9200:9200/tcp 9300:9300/tcp
If anyone interested, GammaGames repo has many updates now, https://github.com/GammaGames/dz . Specially for speed, running the inspect in a single command returns data much faster
# Networks
network_inspect=$(docker network inspect --format "{{slice .Id 0 12}}""\
,{{.Name}}""\
,{{range .IPAM.Config}}{{.Subnet}}{{end}}""\
,{{range .IPAM.Config}}{{if (index . \"Gateway\")}}{{.Gateway}}{{end}}{{end}}" \
"${1}")
# Containers
container_inspect=$(docker container inspect --format "{{slice .Id 0 12}}""\
,{{slice .Name 1}}""\
,{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}""\
,{{range \$p, \$conf := .NetworkSettings.Ports}}{{if \$conf}}{{if ne (index \$conf 0).HostIp \"0.0.0.0\"}}{{(index \$conf 0).HostIp}}:{{end}}{{(index \$conf 0).HostPort}}{{else}}null{{end}}:{{\$p}} {{end}}""\
,{{range \$k, \$v := .NetworkSettings.Networks}}{{\$k}}{{end}}""\
,{{range .NetworkSettings.Networks}}{{.Gateway}} {{end}}" \
${1})
This is the best function I have seen yet.
Some thoughts tho.
And I found the answer by my self, change the row:
container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" "$container_id")"
to
container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}} {{end}}" "$container_id")"
Thanks