13-容器的端口映射
13-容器的端口映射
部署一個簡單web nginx容器
docker run -d --name web nginxnginx 默認的端口是 80 端口,此時我們是沒有辦法訪問的。
好的,通過前面的學(xué)習(xí)我們已經(jīng)知道,這個 web 容器四連接到 bridge 網(wǎng)橋上的,那我們查看一下。
docker network inspect bridge "Containers": {"d6abced642ad8e2be7d7bbb880a8a3bd08414735e3c1151379ac60572e672239": {"Name": "web","EndpointID": "d84522fac6bb4da13b18b9ecc965a803bfdf37b82da4be774d8c490e04b61411","MacAddress": "02:42:ac:11:00:03","IPv4Address": "172.17.0.3/16","IPv6Address": ""},"da991beadf34ef53be9cf3de8f0c5ba1599b76f4433f6627f96c46c09751ecf5": {"Name": "test1","EndpointID": "252a63d6f44927222cdbb2fa761d6cd24120d19a995893b549ccff73803d3a05","MacAddress": "02:42:ac:11:00:02","IPv4Address": "172.17.0.2/16","IPv6Address": ""} },我們可以看到 web 容器確實是連接到 bridge 上的,而且ip地址是 172.17.0.3,那我們再嘗試ping一下這個ip地址。
[vagrant@10 ~]$ ping 172.17.0.3 PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data. 64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.163 ms 64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.047 ms是可以ping通的,那 80 端口是否能訪問呢?
[vagrant@10 ~]$ telnet 172.17.0.3 80 Trying 172.17.0.3... Connected to 172.17.0.3. Escape character is '^]'我們可以看到是能訪問的,那么通過 curl 來訪問 web 的nginx 服務(wù)界面。
[vagrant@10 ~]$ curl 172.17.0.3:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;} </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p> </body> </html>再測試通過本機的80端口看看是否能獲取
[vagrant@10 ~]$ curl 127.0.0.1:80 curl: (7) Failed connect to 127.0.0.1:80; 拒絕連接很顯然是不行的。
我們剛剛通過訪問容器的ip地址成功獲取了 nginx運行的web的html界面。此時我們在本機是可以訪問這個服務(wù)器的,但是其他設(shè)備呢?答案是不能的。
原因很簡單,外界想訪問我們這個容器的80端口,必須要知道 web 容器的 ip地址,但是這個ip地址是私有的,并不能在局域網(wǎng)內(nèi)訪問,只能在本機訪問,那么是否可以通過 本機在局域網(wǎng)內(nèi)的IP地址,例如 192.168.2.133,然后加上80端口訪問呢,答案是不行的。
那如何解決?可以通過在外界訪問 192.168.2.133:80 的時候轉(zhuǎn)發(fā)到 172.17.0.3:80 即可。
這里我們需要用到 docker端口映射 功能。
b4f832fdcfc3c5ddeda380fbbad0be9ca5c64e51868aad17f8b75dc1d3dce263 [vagrant@10 ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b4f832fdcfc3 nginx "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp web da991beadf34 busybox "/bin/sh -c 'while t…" 22 hours ago Up 20 minutes test1-p 參數(shù)為設(shè)定端口映射
測試curl獲取本地80端口是否能夠獲取web內(nèi)容
[vagrant@10 ~]$ curl 127.0.0.1:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;} </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p><p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p> </body> </html>成功獲取。
查看當(dāng)前虛擬機的ip地址
$: ip a 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 08:00:27:16:41:c1 brd ff:ff:ff:ff:ff:ffinet 192.168.2.32/24 brd 192.168.2.255 scope global noprefixroute dynamic eth1valid_lft 5411sec preferred_lft 5411secinet6 fe80::a00:27ff:fe16:41c1/64 scope link valid_lft forever preferred_lft forever然后在筆記本的瀏覽器中訪問 http://192.168.2.32:80/ 我們可以獲取到 web 內(nèi)容了。
這是因為當(dāng)我們訪問虛擬機的80端口時,設(shè)定的端口映射會幫我們轉(zhuǎn)發(fā)到 web 容器的 80 端口上,這樣我們才成功訪問到容器內(nèi)nginx的內(nèi)容。
總結(jié)
以上是生活随笔為你收集整理的13-容器的端口映射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux一键安装node+npm
- 下一篇: Web框架之Django_01初识(三大