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/ \// /'
}
@AntonioCS
Copy link

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.

@scrapix
Copy link

scrapix commented Dec 16, 2022

@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

@scrapix
Copy link

scrapix commented Dec 18, 2022

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 '------------------------------------------------------'

@sanshrestha21
Copy link

[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

@brccabral
Copy link

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 get key and value and print just the key.
container_network=( $(docker inspect --format "{{range \$k, \$v := .NetworkSettings.Networks}}{{printf \"%s\n\" \$k}}{{end}}" "$container_id") )

@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