萌新時(shí)期用過一個(gè)很蠢的方法,在容器里對外訪問一個(gè)web服務(wù)歪今,通過那個(gè)web服務(wù)的日志找到對應(yīng)的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拿到運(yùn)行容器的pid
docker inspect 491bdba6902e|grep -i pid
結(jié)果:
"Pid": 57883,
"PidMode": "",
"PidsLimit": null,
- 通過pid查詢到ip
nsenter -t 57883 -n ip a
結(jié)果:
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