方槍槍的java世界

          不要因?yàn)轱L(fēng)雨飄落就停止了你的腳步,真正的得失就在你的心中。 做喜歡做的事,不輕言放棄!

          Docker學(xué)習(xí)筆記(三)Docker 端口映射

          五、Docker 端口映射

          # Find IP address of container with ID <container_id> 通過(guò)容器 id 獲取 ip $ sudo docker inspect <container_id> | grep IPAddress | cut -d ’"’ -f 4

          無(wú)論如何,這些 ip 是基于本地系統(tǒng)的并且容器的端口非本地主機(jī)是訪問(wèn)不到的。此外,除了端口只能本地訪問(wèn)外,對(duì)于容器的另外一個(gè)問(wèn)題是這些 ip 在容器每次啟動(dòng)的時(shí)候都會(huì)改變。

          Docker 解決了容器的這兩個(gè)問(wèn)題,并且給容器內(nèi)部服務(wù)的訪問(wèn)提供了一個(gè)簡(jiǎn)單而可靠的方法。Docker 通過(guò)端口綁定主機(jī)系統(tǒng)的接口,允許非本地客戶(hù)端訪問(wèn)容器內(nèi)部運(yùn)行的服務(wù)。為了簡(jiǎn)便的使得容器間通信,Docker 提供了這種連接機(jī)制。

          5.1 自動(dòng)映射端口

          -P使用時(shí)需要指定--expose選項(xiàng),指定需要對(duì)外提供服務(wù)的端口

          $ sudo docker run -t -P --expose 22 --name server  ubuntu:14.04

          使用docker run -P自動(dòng)綁定所有對(duì)外提供服務(wù)的容器端口,映射的端口將會(huì)從沒(méi)有使用的端口池中 (49000..49900) 自動(dòng)選擇,你可以通過(guò)docker ps、docker inspect <container_id>或者docker port <container_id> <port>確定具體的綁定信息。

          5.2 綁定端口到指定接口

          基本語(yǔ)法

          $ sudo docker run -p [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp] <image> <cmd>

          默認(rèn)不指定綁定 ip 則監(jiān)聽(tīng)所有網(wǎng)絡(luò)接口。

          綁定 TCP 端口

          # Bind TCP port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1:80:8080 <image> <cmd> # Bind TCP port 8080 of the container to a dynamically allocated TCP port on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1::8080 <image> <cmd> # Bind TCP port 8080 of the container to TCP port 80 on all available interfaces of the host machine. $ sudo docker run -p 80:8080 <image> <cmd> # Bind TCP port 8080 of the container to a dynamically allocated TCP port on all available interfaces $ sudo docker run -p 8080 <image> <cmd>

          綁定 UDP 端口

          # Bind UDP port 5353 of the container to UDP port 53 on 127.0.0.1 of the host machine. $ sudo docker run -p 127.0.0.1:53:5353/udp <image> <cmd>

          posted on 2016-12-27 21:30 做強(qiáng)大的自己 閱讀(196) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 武汉市| 淳安县| 锡林浩特市| 开江县| 长海县| 明星| 长沙县| 虎林市| 绵竹市| 皮山县| 莱西市| 海南省| 观塘区| 韶山市| 乡城县| 涟源市| 玛纳斯县| 松江区| 宽城| 探索| 荔波县| 湛江市| 漠河县| 栾城县| 独山县| 颍上县| 马关县| 封丘县| 山阴县| 遵义县| 同江市| 会同县| 杨浦区| 遂平县| 中卫市| 鄂尔多斯市| 吐鲁番市| 修武县| 米易县| 屏山县| 巴楚县|