萌新时期用过一个很蠢的方法,在容器里对外访问一个web服务,通过那个web服务的日志找到对应的IP地址。。。。
正确姿势
- 先通过docker ps拿到容器的container id
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
554c4578242e module3:v5 "./server" 3 hours ago Up 3 hours 80/tcp keen_poitras
491bdba6902e nginx "/docker-entrypoint.…" 25 hours ago Up 25 hours zealous_euler
- 通过container id拿到运行容器的pid
docker inspect 491bdba6902e|grep -i pid
结果:
"Pid": 57883,
"PidMode": "",
"PidsLimit": null,
- 通过pid查询到ip
nsenter -t 57883 -n ip a
结果:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 9e:29:cf:ef:d2:d8 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.10/16 scope global eth0
valid_lft forever preferred_lft forever
所以nginx的容器被分配的ip地址是:172.17.0.10