端口映射
創(chuàng)新互聯(lián)公司一直秉承“誠信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!為您提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、成都網(wǎng)頁設(shè)計(jì)、成都微信小程序、成都網(wǎng)站開發(fā)、成都網(wǎng)站制作、成都軟件開發(fā)、成都App制作是成都本地專業(yè)的網(wǎng)站建設(shè)和網(wǎng)站設(shè)計(jì)公司,等你一起來見證!
Docker容器啟動之前,如果不進(jìn)行端口映射,在容器外部是無法通過網(wǎng)絡(luò)來訪問容器內(nèi)的網(wǎng)絡(luò)應(yīng)用和服務(wù)。因?yàn)槿萜髦谐3\(yùn)行一些網(wǎng)絡(luò)應(yīng)用和服務(wù),如果想在容器外部通過網(wǎng)絡(luò)來訪問容器內(nèi)的網(wǎng)絡(luò)應(yīng)用和服務(wù),就需要對該容器進(jìn)行端口映射,可以通過docker run命令中的-P或-p選項(xiàng)來進(jìn)行端口映射。隨機(jī)端口映射
當(dāng)通過docker run命令中的-P選項(xiàng)來進(jìn)行端口映射時(shí),Docker會隨機(jī)映射一個(gè)端口到容器開放的網(wǎng)絡(luò)端口。
以nginx為例,先拉取nginx的鏡像。[root@izoq008ryseuupz ~]# docker image pull nginxUsing default tag: latestlatest: Pulling from library/nginx852e50cd189d: Already exists 571d7e852307: Pull complete addb10abd9cb: Pull complete d20aa7ccdb77: Pull complete 8b03f1e11359: Pull complete Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3Status: Downloaded newer image for nginx:latestdocker.io/library/nginx:latest[root@izoq008ryseuupz ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest bc9a0695f571 2 days ago 133MB
再以nginx鏡像創(chuàng)建容器nginx.1,使用-P選項(xiàng)來進(jìn)行隨機(jī)的端口映射。[root@izoq008ryseuupz ~]# docker run -itd -P --name nginx.1 nginx91e3c7ed957f8196fd631eb7bce21acaa96bc253551303ecba2bf193201284ba
通過docker ps -l命令,可以看到本地主機(jī)的32775端口被映射到了容器的80端口。此時(shí)訪問本地主機(jī)的32775端口即可訪問容器內(nèi)nginx應(yīng)用提供的界面。[root@izoq008ryseuupz ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES91e3c7ed957f nginx "/docker-entrypoint.…" 17 minutes ago Up 5 seconds 0.0.0.0:32775->80/tcp nginx.1
通過docker logs nginx.1命令,可以看到剛剛用瀏覽器訪問容器中nginx應(yīng)用提供的界面的記錄。[root@izoq008ryseuupz ~]# docker logs nginx.1...111.8.49.168 - - [27/Nov/2020:09:03:53 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"2020/11/27 09:03:53 [error] 21#21: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 111.8.49.168, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "ip:32775", referrer: "http://ip:32775/"111.8.49.168 - - [27/Nov/2020:09:03:53 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://ip:32775/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"指定端口映射
通過docker run命令中的-p選項(xiàng)來進(jìn)行指定的端口映射。將本地主機(jī)的8083端口映射到容器的80端口。[root@izoq008ryseuupz ~]# docker run -itd -p 8083:80 --name nginx.2 nginx0e62792a194559bf33c2b39f70b64133cb89a48846278ccee651fda68aa6dad1[root@izoq008ryseuupz ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0e62792a1945 nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 0.0.0.0:8083->80/tcp nginx.2
此時(shí)訪問本地主機(jī)的8083端口即可訪問容器內(nèi)nginx應(yīng)用提供的界面。
通過docker logs nginx.2命令,同樣可以看到剛剛用瀏覽器訪問容器中nginx應(yīng)用提供的界面的記錄。[root@izoq008ryseuupz ~]# docker logs nginx.2...111.8.49.168 - - [27/Nov/2020:09:28:32 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"2020/11/27 09:28:32 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 111.8.49.168, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "ip:8083", referrer: "http://ip:8083/"111.8.49.168 - - [27/Nov/2020:09:28:32 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://ip:8083/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
通過docker ps命令,可以看到之前創(chuàng)建的兩個(gè)容器的端口映射都是在本地主機(jī)的0.0.0.0上。[root@izoq008ryseuupz ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0e62792a1945 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:8083->80/tcp nginx.291e3c7ed957f nginx "/docker-entrypoint.…" 53 minutes ago Up 36 minutes 0.0.0.0:32775->80/tcp nginx.1
網(wǎng)上對于0.0.0.0的解釋
首先,0.0.0.0是不能被ping通的。在服務(wù)器中,0.0.0.0并不是一個(gè)真實(shí)的的IP地址,它表示本機(jī)中所有的IPV4地址。監(jiān)聽0.0.0.0的端口,就是監(jiān)聽本機(jī)中所有IP的端口。C:\Users\Kaven>ping 0.0.0.0正在 Ping 0.0.0.0 具有 32 字節(jié)的數(shù)據(jù):PING:傳輸失敗。常見故障。PING:傳輸失敗。常見故障。PING:傳輸失敗。常見故障。PING:傳輸失敗。常見故障。0.0.0.0 的 Ping 統(tǒng)計(jì)信息: 數(shù)據(jù)包: 已發(fā)送 = 4,已接收 = 0,丟失 = 4 (100% 丟失),指定地址的指定端口映射
可以通過ip:hostPort:containerPort這種格式來進(jìn)行指定地址的指定端口映射,比如127.0.0.1:8083。[root@izoq008ryseuupz ~]# docker stop nginx.2nginx.2[root@izoq008ryseuupz ~]# docker run -itd -p 127.0.0.1:8083:80 --name nginx.3 nginxa20b18b97fa4bf7576f6a0d769394a390cfb64ad5fc895453b548bf3f75105ca[root@izoq008ryseuupz ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa20b18b97fa4 nginx "/docker-entrypoint.…" 4 seconds ago Up 4 seconds 127.0.0.1:8083->80/tcp nginx.3
通過docker inspect nginx.3命令,也可以看到端口映射信息。"Ports": { "80/tcp": [ { "HostIp": "127.0.0.1", "HostPort": "8083" } ] }
下面這條命令啟動不了容器,因?yàn)樗鼤J(rèn)使用0.0.0.0,報(bào)錯(cuò)的原因也正是如此 - 地址已在使用中address already in use。docker run -itd -p 8083:80 --name nginx.4 nginx[root@izoq008ryseuupz ~]# docker run -itd -p 8083:80 --name nginx.4 nginxa5211ec678440044945e4d6f16fe59f3ac10367192262768096e7b748ebd027cdocker: Error response from daemon: driver failed programming external connectivity on endpoint nginx.4 (809bed1de2bb89f5d54b5b200503701279a153e60dac08b93e13af8c40a02e36): Error starting userland proxy: listen tcp 0.0.0.0:8083: bind: address already in use.
而下面這條命令卻可以啟動容器。docker run -itd -p 127.0.0.2:8083:80 --name nginx.5 nginx[root@izoq008ryseuupz ~]# docker run -itd -p 127.0.0.2:8083:80 --name nginx.5 nginxf60dcb586968471e7a8127ccef4cb4e89c4198df748da404f78afaff4afa42de
通過docker ps -a命令,可以看到所有容器的一些信息。[root@izoq008ryseuupz ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf60dcb586968 nginx "/docker-entrypoint.…" 19 seconds ago Up 18 seconds 127.0.0.2:8083->80/tcp nginx.5a5211ec67844 nginx "/docker-entrypoint.…" 2 minutes ago Created nginx.4a20b18b97fa4 nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 127.0.0.1:8083->80/tcp nginx.30e62792a1945 nginx "/docker-entrypoint.…" 29 minutes ago Exited (0) 7 minutes ago nginx.291e3c7ed957f nginx "/docker-entrypoint.…" About an hour ago Up 58 minutes 0.0.0.0:32775->80/tcp nginx.1指定地址的隨機(jī)端口映射
可以通過ip::containerPort這種格式來進(jìn)行指定地址的隨機(jī)端口映射,比如127.0.0.1的隨機(jī)端口。[root@izoq008ryseuupz ~]# docker run -itd -p 127.0.0.1::80 --name nginx.6 nginx396b7d9e64c047187c0391485ba1d275587d86409573e882bc68a9357dfcb6f2[root@izoq008ryseuupz ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES396b7d9e64c0 nginx "/docker-entrypoint.…" 9 seconds ago Up 9 seconds 127.0.0.1:32768->80/tcp nginx.6
可以看到Docker將127.0.0.1的32768端口映射到了容器的80端口,而這個(gè)32768端口是由Docker隨機(jī)指定的。多個(gè)端口映射
使用多個(gè)-p選項(xiàng)即可進(jìn)行多個(gè)端口映射。[root@izoq008ryseuupz ~]# docker run -itd -p 9091:9091 -p 9092:8080 -p 9093:8083 --name nginx.8 nginx5ac6438dcc2a2d1b7f668eb10d8285085c396a44f896aae0b3dc09935e4e95eb[root@izoq008ryseuupz ~]# docker ps -lCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES5ac6438dcc2a nginx "/docker-entrypoint.…" 6 seconds ago Up 4 seconds 80/tcp, 0.0.0.0:9091->9091/tcp, 0.0.0.0:9092->8080/tcp, 0.0.0.0:9093->8083/tcp nginx.8查看端口映射配置
docker port命令可以查看容器的端口映射配置。[root@izoq008ryseuupz ~]# docker port nginx.6 80127.0.0.1:32768[root@izoq008ryseuupz ~]# docker port nginx.5 80127.0.0.2:8083[root@izoq008ryseuupz ~]# docker port nginx.380/tcp -> 127.0.0.1:8083[root@izoq008ryseuupz ~]# docker port nginx.88080/tcp -> 0.0.0.0:90928083/tcp -> 0.0.0.0:90939091/tcp -> 0.0.0.0:9091
Docker容器的端口映射就介紹到這里。
到此這篇關(guān)于Docker初級網(wǎng)絡(luò)端口映射的配置的文章就介紹到這了,更多相關(guān)Docker 端口映射內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
文章名稱:Docker初級網(wǎng)絡(luò)端口映射的配置
瀏覽路徑:http://www.rwnh.cn/article40/icdjeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站、網(wǎng)站排名、微信小程序、服務(wù)器托管、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)