我正在构建一个Docker(1.9.1)网络并为其附加一些容器。
docker network inspect NETWORK
列出了已连接的容器,但仅使用容器ID引用它们,而我还想查看它们的名称。
我没有找到docker network inspect
命令的选项来实现此目的。正在解析外部,例如使用脚本,此命令的输出与docker ps
唯一的当前输出方式一起输出?
编辑这是我在python(2.7)中编写的帮助器,用于识别与docker网络中的容器相关联的IP地址(将网络名称作为参数传递):
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']
答案 0 :(得分:2)
我使用这个脚本:
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