Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List Docker Container Names and IPs
function drips(){
docker ps -q | xargs -n 1 docker inspect --format '{{ .NetworkSettings.IPAddress }} {{ .Name }}' | sed 's/ \// /'
}
@ENInms

This comment has been minimized.

Copy link

@ENInms ENInms commented Oct 8, 2017

and if you are (fortunate enough to be?) on Windows:

(docker ps -q | ForEach-Object { docker inspect $_ --format '{{ .NetworkSettings.IPAddress }} {{ .Name }}' }).replace('/','')
@zeroows

This comment has been minimized.

Copy link

@zeroows zeroows commented Oct 29, 2017

To Show all ips and name in the new docker

docker ps -q | xargs -n 1 docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{ .Name }}' | sed 's/ \// /'
@GammaGames

This comment has been minimized.

Copy link

@GammaGames GammaGames commented Oct 19, 2018

I wanted to get the container's port as well, so I added the following to my .bashrc:

function dip() {
        if [ -z $1 ]; then
                docker ps -a --format "{{.ID}}" | while read -r line ; do
                        echo $line $(docker inspect --format "{{ .Name }} {{ .NetworkSettings.Networks.bridge.IPAddress }}" $line | sed 's/\///'):$(docker port "$line" | grep -o "0.0.0.0:.*" | cut -f2 -d:)
                done
        else
                echo $(docker inspect --format "{{.ID }} {{ .Name }} {{ .NetworkSettings.Networks.bridge.IPAddress }}" $1 | sed 's/\///'):$(docker port "$1" | grep -o "0.0.0.0:.*" | cut -f2 -d:)
        fi
}

Running dip in the command line prints out all container's ID, name, IP, and port, and running dip [id] prints out just that container's ID, name, IP, and port.

@AntonioCS

This comment has been minimized.

Copy link

@AntonioCS AntonioCS commented Jan 31, 2020

This is what worked for me:

docker ps -q | xargs -n 1 docker inspect --format '{{ .Name }} {{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' | sed 's#^/##';

Got it here: https://bytefreaks.net/applications/docker/how-to-list-all-docker-container-names-and-their-ips

@16g

This comment has been minimized.

Copy link

@16g 16g commented Jul 7, 2020

@

This is what worked for me:

docker ps -q | xargs -n 1 docker inspect --format '{{ .Name }} {{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}' | sed 's#^/##';

Got it here: https://bytefreaks.net/applications/docker/how-to-list-all-docker-container-names-and-their-ips

Thank you! Its worked for me too

@yebo29

This comment has been minimized.

Copy link

@yebo29 yebo29 commented Mar 15, 2021

I wanted to get the container's port as well, so I added the following to my .bashrc:

function dip() {
        if [ -z $1 ]; then
                docker ps -a --format "{{.ID}}" | while read -r line ; do
                        echo $line $(docker inspect --format "{{ .Name }} {{ .NetworkSettings.Networks.bridge.IPAddress }}" $line | sed 's/\///'):$(docker port "$line" | grep -o "0.0.0.0:.*" | cut -f2 -d:)
                done
        else
                echo $(docker inspect --format "{{.ID }} {{ .Name }} {{ .NetworkSettings.Networks.bridge.IPAddress }}" $1 | sed 's/\///'):$(docker port "$1" | grep -o "0.0.0.0:.*" | cut -f2 -d:)
        fi
}

Running dip in the command line prints out all container's ID, name, IP, and port, and running dip [id] prints out just that container's ID, name, IP, and port.

Expansion of this:

function dip() {
        if [ -z $1 ]; then
                docker ps -a --format "{{.ID}}" | while read -r line ; do
                        PORTS=$(docker port "$line" | grep -o "0.0.0.0:.*" | cut -f2 -d:)
                        if [ -z "${PORTS}" ]; then
                            echo $line $(docker inspect --format "{{ .Name }} {{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}" $line | sed 's/\///'):" No open ports"
                        else
                            echo $line $(docker inspect --format "{{ .Name }} {{range .NetworkSettings.Networks}} {{.IPAddress}}{{end}}" $line | sed 's/\///'):${PORTS}
                        fi
                done
        else
                echo $(docker inspect --format "{{.ID }} {{ .Name }} {{ .NetworkSettings.Networks.bridge.IPAddress }}" $1 | sed 's/\///'):$(docker port "$1" | grep -o "0.0.0.0:.*" | cut -f2 -d:)
        fi
}

Will report "no open ports" for containers that are not exposing ports to the outside world. Also fixed an issue on my docker cli Docker version 20.10.5, build 55c4c88where the IP was not showing up using a previous comment.

Sample output:

billybob@Pi4:~ $ dip
98f0bc13fde0 portainer 172.xx.xx.xx:8000 9000
fb50db9e9e3d rpi-docker-bob_1 172.xx.xx.2: No open ports
@Looooopy

This comment has been minimized.

Copy link

@Looooopy Looooopy commented Apr 13, 2021

A formatted extended version

function dip() {
        _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 "%-13s %-40s %-20s %-80s\n" "$container_id" "$container_name" "$container_ip" "${container_ports[*]}"
        }

        local container_id
        container_id="$1"
        printf "%-13s %-40s %-20s %-80s\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
}

Sample output

bash-5.0$ dip
Container Id  Container Name                           Container IP         Container Ports
09c4a002720b  ab_rabbitbus_1                           172.64.0.8           25672 5671 5672 15671 15672
@yebo29

This comment has been minimized.

Copy link

@yebo29 yebo29 commented Apr 13, 2021

A formatted extended version

function dip() {
        _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 "%-13s %-40s %-20s %-80s\n" "$container_id" "$container_name" "$container_ip" "${container_ports[*]}"
        }

        local container_id
        container_id="$1"
        printf "%-13s %-40s %-20s %-80s\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
}

Sample output

bash-5.0$ dip
Container Id  Container Name                           Container IP         Container Ports
09c4a002720b  ab_rabbitbus_1                           172.64.0.8           25672 5671 5672 15671 15672

NICE! Thanks!

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