giavac
giavac

Reputation: 1008

Docker networking: how to show connected containers names

I'm building a Docker (1.9.1) network and attaching a few containers to it. docker network inspect NETWORK lists the connected containers, but refers to them only with the containers IDs, while I'd like to see also their names.

I haven't found an option for the docker network inspect command to achieve this. Is parsing externally, e.g. with a script, the output of this command together with the output of docker ps the only current way?

EDIT This is what I've written in python (2.7) as an helper to identify the IP address associated to containers inside a docker network (passing the network name as argument):

import json
import subprocess
import sys

network_name = sys.argv[1]
network_json = subprocess.check_output(["docker", "network", "inspect", network_name])
network = json.loads(network_json)
containers = network[0]['Containers']

for container_id in containers:
    container_name = subprocess.check_output(["docker", "inspect", "--format", "'{{ .Name }}'", container_id])
    print container_id + " " + container_name.strip() + ": " + containers[container_id]['IPv4Address']

Upvotes: 4

Views: 1300

Answers (1)

Chris
Chris

Reputation: 103

I use this script:

 if [ $# -eq 0 ]; then
    nw=($(docker network ls | awk 'NR>=2 {printf "%s ",$2}'))
else
    nw=$@
fi

echo ${nw[@]}
for network in ${nw[@]};do

    docker network ls | grep $network 1>/dev/null 2>/dev/null
    if [ $? -ne 0 ]; then
        echo -e "\nNetwork $network not found!"
        exit 1
    fi

    ids=$(docker network inspect $network | grep -E "^\s+\".{64}\"" | awk -F\" '{printf "%s ",substr($2,0,12)}')
    echo -e "\nContainers in network $network:"
    for id in ${ids[@]}; do
        name=$(docker ps -a | grep $id | awk '{print $NF}')
        echo -e "\t$id: $name"
    done
    shift
done

Upvotes: 2

Related Questions