宿主机如何访问容器内部网络
我们知道,启动容器时,选择一个端口映射到容器内部开放端口上,例如:docker run --name kanga -p 127.0.0.1:9090:9090 -v kanga:/home/ljs/go/src/kanga -v kanga_export:/home/ljs/export -tid kanga:go /bin/bash /home/ljs/go/src/kanga/start...
·
我们知道,启动容器时,选择一个端口映射到容器内部开放端口上,例如:
docker run --name kanga -p 127.0.0.1:9090:9090 -v kanga:/home/ljs/go/src/kanga -v kanga_export:/home/ljs/export -tid kanga:go /bin/bash /home/ljs/go/src/kanga/start.sh
启动容器后,相当于启动了容器内部的web服务,那么在宿主机上访问该服务:
curl -H "Host: nasa_test.store.n.netease.com" http://127.0.0.1:9090/ljs.jpg?fit=crop\&w=2000\&h=1000\&crop=bottom > crop_bottom.jpg
对于容器内的web服务来说,那么究竟用的是哪个ip地址来访问的呢?该容器的服务是自研的,可以记录下访问时的ip地址,那么我们通过日志来看看,具体是用哪个ip地址来访问该服务的?
2019-02-13T06:45:27.788183687Z 172.17.0.1 nasa_test.store.n.netease.com 'GET /ljs.jpg?fit=crop&w=2000&h=1000&crop=bottom' 200 401448 240.451556ms
从上面日志可以看到,访问该服务使用的ip是172.17.0.1,这个ip地址是谁的呢?用ifconfig分别查看容器和宿主机:
# docker
[ljs@51a49e226cd4 ~]$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 96 bytes 277070 (270.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 54 bytes 405149 (395.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 4 bytes 200 (200.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4 bytes 200 (200.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# host
docker0 Link encap:Ethernet HWaddr 02:42:eb:74:da:62
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:ebff:fe74:da62/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:233 errors:0 dropped:0 overruns:0 frame:0
TX packets:356 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:514827 (514.8 KB) TX bytes:569403 (569.4 KB)
...
从上面我们可以知道,172.17.0.1这个ip地址是宿主机的虚拟网卡ip,也就是说,宿主机是通过docker0这个网卡来访问docker内部网络的。
更多推荐
已为社区贡献3条内容
所有评论(0)