Skip to content

Instantly share code, notes, and snippets.

@ipedrazas
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/ \// /'
}
@brccabral
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_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

}

@GammaGames
Copy link

GammaGames commented Feb 24, 2024

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

@brccabral
Copy link

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})

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