Skip to content

Instantly share code, notes, and snippets.

@mrjk
Last active May 25, 2023 13:16
Show Gist options
  • Save mrjk/1f751ffec318cf7485cca9946e01e43b to your computer and use it in GitHub Desktop.
Save mrjk/1f751ffec318cf7485cca9946e01e43b to your computer and use it in GitHub Desktop.
Docker Scripts
#!/bin/bash
# From: https://gist.github.com/ipedrazas/2c93f6e74737d1f8a791?permalink_comment_id=3704504#gistcomment-3704504
set -eu
function dip() {
local cols='%-13s %-48s %-40s %-80s'
_print_container_info() {
local container_id
local container_ports
local container_ip
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" )"
printf "$cols\n" "$container_id" "$container_name" "$container_ip" "${container_ports[*]}"
}
local container_id
container_id="$1"
printf "$cols\n" 'Container Id' 'Container Name' 'Container IP' 'Container Ports'
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
}
dip

List of useful docker scripts

  • netshoot: Attach a netshoot container to existing instance
  • dip: Docker IPs, show all containers IPs
  • nip: Network IPs, show all containers IPs per networks
  • dports: Docker ports, list all exposed ports
#!/bin/bash
# From: https://gist.github.com/ipedrazas/2c93f6e74737d1f8a791?permalink_comment_id=3704504#gistcomment-3704504
function dip() {
local cols='%-13s %-48s %-40s %-80s'
_print_container_info() {
local container_id
local container_ports
local container_ip
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" )"
printf "$cols\n" "$container_id" "$container_name" "$container_ip" "${container_ports[*]}"
}
local container_id
container_id="$1"
printf "$cols\n" 'Container Id' 'Container Name' 'Container IP' 'Container Ports'
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
}
dip
#!/bin/bash
# List all containers with their exposed ports
set -eu
list_ports ()
{
local raw=
while IFS=';' read -r cont ports _ ; do
#echo "Ports: $ports"
ports=${ports//,/}
for port in $ports; do
if ! [[ "$port" =~ '->' ]]; then
port=".->${port}"
fi
port=${port//->/ }
echo -e "$port\t$cont"
done
done <<< $(docker container ls --format "{{.Names}};{{.Ports}}" -a)
}
main ()
{
list_ports | sort | column -t
}
main $@
#!/bin/bash
# Start a docker netshoot container
set -eu
main ()
{
local container=${1-}
local args=
local opts=
if [[ -n "$container" ]]; then
opts="--network container:$container"
fi
docker run --rm -ti -v "$HOME/.cache/netshoot:/root" $opts nicolaka/netshoot /bin/bash
}
main $@
#!/bin/bash
# Allow to list docker networks with their associated containers and IPs.
# Usage: Use optional extra argument to filter container names
# Syntax: nip [BASH_REGEX]
# Exemples:
# nip
# nip 'traefik'
# nip 'traef|wordpress'
# Install: curl https://gist.githubusercontent.com/mrjk/d99b6cba40f16592e3f32f363de71cea/raw/e1e91f146cb5487c6c7bc7843b23d7d7fc311894/nip > /usr/local/bin/nip && chmod +x /usr/local/bin/nip
# Require: jq
# License: MIT
list_networks ()
{
local filter="${1:-.*}"
# List all Docker networks
local networks=$(docker network ls -q --format '{{.ID}}')
# Loop through the networks and display the details
for network in $networks; do
local containers=$(docker network inspect --format='{{range $k, $v := .Containers}}{{$k}} {{end}}' "$network")
local net_name=$(docker network inspect --format '{{ .Name }}' $network)
printf "%-30s %s\n" "$net_name" "$network"
# Loop over each container
for container in $containers; do
local cont=$(docker network inspect --format="{{ index .Containers \"$container\" | json }}" "$network" )
local name=$(jq -r ".Name" <<< "$cont")
if [[ "$name" =~ $filter ]]; then
local ip=$(jq -r ".IPv4Address" <<< "$cont")
printf " %-30s %s\n" "$ip" "$name"
fi
done
done
}
list_networks ${@-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment