创建新容器后,是否可以运行命令从主机获取容器的IP地址?
基本上,一旦Docker创建了容器,我就想滚动我自己的代码部署和容器配置脚本。
创建新容器后,是否可以运行命令从主机获取容器的IP地址?
基本上,一旦Docker创建了容器,我就想滚动我自己的代码部署和容器配置脚本。
当前回答
我编写了以下Bash脚本,以从docker compose下运行的所有容器中获取IP地址表。
function docker_container_names() {
docker ps -a --format "{{.Names}}" | xargs
}
# Get the IP address of a particular container
dip() {
local network
network='YOUR-NETWORK-HERE'
docker inspect --format "{{ .NetworkSettings.Networks.$network.IPAddress }}" "$@"
}
dipall() {
for container_name in $(docker_container_names);
do
local container_ip=$(dip $container_name)
if [[ -n "$container_ip" ]]; then
echo $(dip $container_name) " $container_name"
fi
done | sort -t . -k 3,3n -k 4,4n
}
您应该将变量网络更改为自己的网络名称。
其他回答
在Docker 1.3+中,您也可以使用以下方法进行检查:
输入正在运行的Docker(Linux):
docker exec [container-id or container-name] cat /etc/hosts
172.17.0.26 d8bc98fa4088
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.17 mysql
对于窗口:
docker exec [container-id or container-name] ipconfig
下面是一个快速有效的答案:
获取您的容器名称或ID:
docker container ls
然后获取IP:
docker inspect <container_ID Or container_name> |grep 'IPAddress'
获取端口:
docker inspect <container_ID Or container_name> |grep 'Port'
inspect的--format选项起了作用。
现代Docker客户端语法为:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
旧Docker客户端语法为:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_name_or_id
这些命令将返回Docker容器的IP地址。
如评论中所述:如果您使用Windows,请在大括号周围使用双引号“而不是单引号”。
您可以使用docker inspect<container id>。
例如:
CID=$(docker run -d -p 4321 base nc -lk 4321);
docker inspect $CID
接受的答案对于每个容器的多个网络不适用:
> docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' cc54d96d63ea
172.20.0.4172.18.0.5
下一个最佳答案更接近:
> docker inspect cc54d96d63ea | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "",
"IPAddress": "172.20.0.4",
"IPAddress": "172.18.0.5",
我喜欢使用jq解析网络JSON:
> docker inspect cc54d96d63ea | jq -r 'map(.NetworkSettings.Networks) []'
{
"proxy": {
"IPAMConfig": null,
"Links": [
"server1_php_1:php",
"server1_php_1:php_1",
"server1_php_1:server1_php_1"
],
"Aliases": [
"cc54d96d63ea",
"web"
],
"NetworkID": "7779959d7383e9cef09c970c38c24a1a6ff44695178d314e3cb646bfa30d9935",
"EndpointID": "4ac2c26113bf10715048579dd77304008904186d9679cdbc8fcea65eee0bf13b",
"Gateway": "172.20.0.1",
"IPAddress": "172.20.0.4",
"IPPrefixLen": 24,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:14:00:04",
"DriverOpts": null
},
"webservers": {
"IPAMConfig": null,
"Links": [
"server1_php_1:php",
"server1_php_1:php_1",
"server1_php_1:server1_php_1"
],
"Aliases": [
"cc54d96d63ea",
"web"
],
"NetworkID": "907a7fba8816cd0ad89b7f5603bbc91122a2dd99902b504be6af16427c11a0a6",
"EndpointID": "7febabe380d040b96b4e795417ba0954a103ac3fd37e9f6110189d9de92fbdae",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.5",
"IPPrefixLen": 24,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:05",
"DriverOpts": null
}
}
要列出每个容器的IP地址,则变成:
for s in `docker ps -q`; do
echo `docker inspect -f "{{.Name}}" ${s}`:
docker inspect ${s} | jq -r 'map(.NetworkSettings.Networks) []' | grep "IPAddress";
done
/server1_web_1:
"IPAddress": "172.20.0.4",
"IPAddress": "172.18.0.5",
/server1_php_1:
"IPAddress": "172.20.0.3",
"IPAddress": "172.18.0.4",
/docker-gen:
"IPAddress": "172.18.0.3",
/nginx-proxy:
"IPAddress": "172.20.0.2",
"IPAddress": "172.18.0.2",