Skip to content

Instantly share code, notes, and snippets.

Created September 18, 2014 22:13
Show Gist options
  • Save ipedrazas/2c93f6e74737d1f8a791 to your computer and use it in GitHub Desktop.
Save ipedrazas/2c93f6e74737d1f8a791 to your computer and use it in GitHub Desktop.
List Docker Container Names and IPs
function drips(){
docker ps -q | xargs -n 1 docker inspect --format '{{ .NetworkSettings.IPAddress }} {{ .Name }}' | sed 's/ \// /'
Copy link

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_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
    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"
        # 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"

    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_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
    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"
        # 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"

    column -s "," --output-separator "" -t ~/tmp/docker-container.txt
    echo '------------------------------------------------------'

    rm -r ~/tmp/docker-container.txt


Copy link

GammaGames commented Feb 24, 2024

Anyone wanna add these changes to the repo version?
It’s also got a command to sh into containers.

showing local ports might be useful too, sometimes I bind stuff to 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 since your changes made that way easier to implement, here's what that looks like with a bunch of different configs:

Container Ports
9200:9200/tcp 9300:9300/tcp

Copy link

If anyone interested, GammaGames repo has many updates now, . 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}}""\
,{{range .IPAM.Config}}{{.Subnet}}{{end}}""\
,{{range .IPAM.Config}}{{if (index . \"Gateway\")}}{{.Gateway}}{{end}}{{end}}" \
# 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 \"\"}}{{(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}}" \

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment