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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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!

@mats-nk
Copy link

mats-nk commented Oct 3, 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!

This is the best function I have seen yet.

Some thoughts tho.

  1. Its slow
  2. Could it be changed to handle the situation when a containers have several IPs ?
    And I found the answer by my self, change the row:
container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" "$container_id")"

to

container_ip="$(docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}  {{end}}" "$container_id")"

Thanks

@video-online
Copy link

docker ps -q | xargs -n 1 docker inspect --format '{{ .NetworkSettings.Networks.dockernet.IPAddress }} {{ .Name }}' | sed 's/ // /'

@kerphi
Copy link

kerphi commented Sep 19, 2022

A dip extended command listing container working dir + container image:

function dip() {
        _print_container_info() {
            local container_id
            local container_ports
            local container_ip
            local container_name
            local container_image
            container_id="${1}"
        
            container_ports=( $(sudo docker port "$container_id" | grep -o "0.0.0.0:.*" | cut -f2 -d:) )
            container_name="$(sudo docker inspect --format "{{ .Name }}" "$container_id" | sed 's/\///')"
            container_workdir="$(sudo docker inspect --format '{{index .Config.Labels "com.docker.compose.project.working_dir" }}' "$container_id")"
            container_ip="$(sudo docker inspect --format "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}" "$container_id")"
            container_image="$(sudo docker inspect --format "{{ .Config.Image }}" "$container_id" | sed 's/\///')"
            printf "%-40s %-50s %-20s %-20s %-72s\n" "$container_workdir" "$container_name" "$container_ip" "${container_ports[*]}" "$container_image"
        }

        local container_id
        container_id="$1"
        printf "%-40s %-50s %-20s %-20s %-72s\n" 'Container directory' 'Container Name' 'Container IP' 'Container Ports' 'Container Image'
        if [ -z "$container_id" ]; then
            local container_id
            sudo sudo docker ps -a --format "{{.Names}} {{.ID}}" | sort | sed 's#^[^ ]\+ ##g' | while read -r container_id ; do
                _print_container_info  "$container_id"
            done
        else
            _print_container_info  "$container_id"
        fi
}

output example:

Container directory                      Container Name                                     Container IP         Container Ports      Container Image
/opt/pod/abes-filebeat-docker            abes-filebeat                                      192.168.192.2                             docker.elastic.cobeats/filebeat:7.17.0
/opt/pod/abes-hello-docker               abes-hello-batch                                   172.30.0.2                                abesesrabes-hello:develop-batch
/opt/pod/abes-hello-docker               abes-hello-front                                   172.30.0.3           7080                 abesesrabes-hello:develop-front
/opt/pod/abes-hello-docker               abes-hello-web                                     172.30.0.4           7081                 abesesrabes-hello:develop-web
/opt/pod/microwebservices-docker         bacon-cache-warmer-kbart-withdate                  192.168.48.6                              abesesrbacon-cache-warmer:1.3.1
/opt/pod/microwebservices-docker         bacon-cache-warmer-kbart-withoutdate               192.168.48.5                              abesesrbacon-cache-warmer:1.3.1

@a1ea321
Copy link

a1ea321 commented Nov 16, 2022

The solutions I see here spawn unnecessarily many processes. (Well, necessary for readable code but not absolutely necessary for the functionality.) I've developed this piece of abomination:

dip () {
    docker_ps="$(
        docker ps --format 'table{{.Names}}|{{.Status}}|{{.Size}}' |
        tail --lines +2 | 
        sort
    )"

    # https://stackoverflow.com/a/20686101
    FORMAT='{{.Name}}|{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' 

    ip_addresses=`
        echo "$docker_ps" |
        cut --fields 1 --delimiter '|' |
        while read -r container; do
            docker inspect --format=$FORMAT $container
        done |
        sed 's|^/||' |  
        sort
    `
    
    { 
        FM="\033[1m%s\033[0m|\033[1m%s\033[0m|\033[1m%s\033[0m|\033[1m%s\033[0m"
        printf "$FM\n" NAMES IP STATUS SIZE
        join -t "|" <(echo "$ip_addresses") <(echo "$docker_ps")
    } | 
    column --separator "|" --output-separator "┊" --table
}

Sample output:

NAMES  ┊IP        ┊STATUS      ┊SIZE
dnsmasq┊172.18.0.2┊Up 7 hours  ┊2B (virtual 6.64MB)
esphome┊172.19.0.2┊Up 5 minutes┊422kB (virtual 270MB)

Notes:

  • I cannot format it here but the header row is shown bold in the terminal.
  • You'll probably have to modify this a bit it according to your favorite fields.
  • I wouldn't be surprised if docker inspect does the job on its own with a contrived enough formatting string. But this was my very first real world use of the join command.
  • I thought replacing while read -r container with xargs would squeeze out some more speed; but time says otherwise.
  • My code also spawns too many processes, but at least just two docker processes. Since the docker commands are the expensive part, my code is superior to everything else here. Not. I timed the stuff here against my code, and my code is slower heh heh heh. But docker may be caching results, making my measurement method faulty. Probably not.
  • Manually adding the header goes against the single source of truth principle: You have to modify two places in order to get one modification. But saves some spawns (and bash-induced-headaches).

@GammaGames
Copy link

Wow, I hadn't realized people were updating this! Earlier this year I moved the command into a little bash script so I could add some extra helpers like tailing logs and connecting with bash (and so I didn't have to manually sync the changes between machines, just a simple pull): https://github.com/GammaGames/dz
I just updated it with some of your improvements 😄️

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

@shresthasan
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

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