最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

docker 動態(tài)映射運行的container端口實例詳解

 更新時間:2016年10月27日 11:43:28   投稿:lqh  
這篇文章主要介紹了 docker 動態(tài)映射運行的container端口實例詳解的相關(guān)資料,需要的朋友可以參考下

docker動態(tài)映射運行的container端口,最近做項目,對于docker動態(tài)映射運行的container端口的資料有必要記錄下,以便以后在用到,

Docker自帶了EXPOSE命令,可以通過編寫dockerfile加-p參數(shù)方便的映射Container內(nèi)部端口,但是對于已經(jīng)運行的container,如果你想對外開放一個新的端口,只能編輯dockerfile然后重新build,有點不太方便。

其實docker本身使用了iptables來做端口映射的,所以我們可以通過一些簡單的操作來實現(xiàn)動態(tài)映射運行中的container端口。

通過運行iptables命令可以看到具體的端口映射(下面的實例中IP為192.168.42.41的container開放了22和20280等端口)

[yaxin@ubox ~]$sudo iptables -nvxL 
Chain INPUT (policy ACCEPT 262 packets, 529689 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
  14355  789552 DROP    tcp -- *   *    0.0.0.0/0      0.0.0.0/0      tcp dpt:25 
 
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 5479459 653248187 DOCKER   all -- *   docker0 0.0.0.0/0      0.0.0.0/0 
  93990 314970368 ACCEPT   all -- *   docker0 0.0.0.0/0      0.0.0.0/0      ctstate RELATED,ESTABLISHED 
 4705395 2183219154 ACCEPT   all -- docker0 !docker0 0.0.0.0/0      0.0.0.0/0 
    0    0 ACCEPT   all -- docker0 docker0 0.0.0.0/0      0.0.0.0/0 
 
Chain OUTPUT (policy ACCEPT 282 packets, 622495 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 
Chain DOCKER (1 references) 
  pkts   bytes target   prot opt in   out   source        destination 
   218  13193 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:22280 
 4868186 297463902 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:20280 
  78663 13128102 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:22 
   47   4321 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:28159 
[yaxin@ubox ~]$sudo iptables -t nat -nvxL 
Chain PREROUTING (policy ACCEPT 210199 packets, 14035875 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 1219483 82563968 DOCKER   all -- *   *    0.0.0.0/0      0.0.0.0/0      ADDRTYPE match dst-type LOCAL 
 
Chain INPUT (policy ACCEPT 197679 packets, 13316595 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 
Chain OUTPUT (policy ACCEPT 271553 packets, 16671466 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
  1643  99251 DOCKER   all -- *   *    0.0.0.0/0      !127.0.0.0/8     ADDRTYPE match dst-type LOCAL 
 
Chain POSTROUTING (policy ACCEPT 271743 packets, 16682594 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
  13468  811013 MASQUERADE all -- *   !docker0 192.168.42.0/24   0.0.0.0/0 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:22280 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:20280 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:22 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:28159 
 
Chain DOCKER (2 references) 
  pkts   bytes target   prot opt in   out   source        destination 
   22   1404 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:22280 to:192.168.42.41:22280 
   288  17156 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:20280 to:192.168.42.41:20280 
   93   5952 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:22222 to:192.168.42.41:22 
    8   512 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:28159 to:192.168.42.41:28159 



我們要做的就是根據(jù)自己container的情況配置iptables規(guī)則。

首先是filter這個表,我們要配置其放通轉(zhuǎn)發(fā),docker默認(rèn)已經(jīng)將所有的FORWARD規(guī)則放到了DOCKER這個自建鏈(chain)中了,這樣方便配置,也方便查看。

然后再配置nat表為其配置DNAT,這個才是端口轉(zhuǎn)發(fā)的核心配置。這個表中只需要配置POSTROUTING和DOCKER鏈即可,這里不講為什么這么配置,如果想要深入了解iptables請google一下。

下面舉個例子:

假如我有一個container,名字為nginx(通過運行docker ps命令即可查詢),現(xiàn)在我在docker內(nèi)部運行nginx程序,監(jiān)聽了8888端口,我希望外網(wǎng)可以通過8899端口(注意一下端口)訪問

找到docker為nginx分配的IP

[yaxin@ubox ~]$sudo docker inspect -f '{{.NetworkSettings.IPAddress}}' nginx
192.168.42.43

配置iptables中filter表的FORWARD(DOCKER)鏈

[yaxin@ubox ~]$sudo iptables -A DOCKER ! -i docker0 -o docker0 -p tcp --dport 8888 -d 192.168.42.43 -j ACCEPT


配置iptables中nat表的PREROUTING(DOCKER)和POSTROUTING鏈

[yaxin@ubox ~]$sudo iptables -t nat -A POSTROUTING -p tcp --dport 8888 -s 192.168.42.43 -d 192.168.42.43 -j MASQUERADE
[yaxin@ubox ~]$sudo iptables -t nat -A DOCKER ! -i dokcer0 -p tcp --dport 8899 -j DNAT --to-destination 192.168.42.43:8888


通過外網(wǎng)訪問curl http://IP:8899就會顯示nginx下配置的html頁面

最后iptables規(guī)則

[yaxin@ubox ~]$sudo iptables -nvxL 
Chain INPUT (policy ACCEPT 67893 packets, 212661547 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
  14364  790008 DROP    tcp -- *   *    0.0.0.0/0      0.0.0.0/0      tcp dpt:25 
 
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 5479682 653269356 DOCKER   all -- *   docker0 0.0.0.0/0      0.0.0.0/0 
  94186 314986910 ACCEPT   all -- *   docker0 0.0.0.0/0      0.0.0.0/0      ctstate RELATED,ESTABLISHED 
 4705658 2183254076 ACCEPT   all -- docker0 !docker0 0.0.0.0/0      0.0.0.0/0 
    0    0 ACCEPT   all -- docker0 docker0 0.0.0.0/0      0.0.0.0/0 
 
Chain OUTPUT (policy ACCEPT 71253 packets, 222512872 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 
Chain DOCKER (1 references) 
  pkts   bytes target   prot opt in   out   source        destination 
   218  13193 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:22280 
 4868186 297463902 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:20280 
  78663 13128102 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:22 
   47   4321 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.41    tcp dpt:28159 
   27   4627 ACCEPT   tcp -- !docker0 docker0 0.0.0.0/0      192.168.42.43    tcp dpt:8888 
[yaxin@ubox ~]$sudo iptables -t nat -nvxL 
Chain PREROUTING (policy ACCEPT 232 packets, 16606 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 1220281 82620790 DOCKER   all -- *   *    0.0.0.0/0      0.0.0.0/0      ADDRTYPE match dst-type LOCAL 
 
Chain INPUT (policy ACCEPT 216 packets, 15671 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
 
Chain OUTPUT (policy ACCEPT 317 packets, 19159 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
  1644  99311 DOCKER   all -- *   *    0.0.0.0/0      !127.0.0.0/8     ADDRTYPE match dst-type LOCAL 
 
Chain POSTROUTING (policy ACCEPT 321 packets, 19367 bytes) 
  pkts   bytes target   prot opt in   out   source        destination 
  13512  813656 MASQUERADE all -- *   !docker0 192.168.42.0/24   0.0.0.0/0 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:22280 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:20280 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:22 
    0    0 MASQUERADE tcp -- *   *    192.168.42.41    192.168.42.41    tcp dpt:28159 
    0    0 MASQUERADE tcp -- *   *    192.168.42.43    192.168.42.43    tcp dpt:8888 
 
Chain DOCKER (2 references) 
  pkts   bytes target   prot opt in   out   source        destination 
   22   1404 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:22280 to:192.168.42.41:22280 
   288  17156 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:20280 to:192.168.42.41:20280 
   93   5952 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:22222 to:192.168.42.41:22 
    8   512 DNAT    tcp -- !docker0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:28159 to:192.168.42.41:28159 
    4   208 DNAT    tcp -- !dokcer0 *    0.0.0.0/0      0.0.0.0/0      tcp dpt:8899 to:192.168.42.43:8888 

當(dāng)然,使用手動配置也是比較麻煩的,所以我寫了一個腳本來自動配置端口映射,使用方法腳本中有說明

#!/bin/bash 
# filename: docker_expose.sh 
 
if [ `id -u` -ne 0 ];then 
  echo "[EROOR] Please use root to run this script" 
  exit 23 
fi 
 
if [ $# -ne 3 ];then 
  echo "Usage: $0 <container_name> <add|del> [[<machine_ip>:]<machine_port>:]<container_port>[/<protocol_type>]" 
  exit 1 
fi 
 
IPV4_RE='(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])' 
 
container_name=$1 
action=$2 
arguments=$3 
 
# check action 
if [ "$action"x != "add"x -a "$action"x != "del"x ];then 
  echo "[ERROR] Please use add or del parameter to add port map or delete port map" 
  exit 654 
fi 
if [ "$action"x == "add"x ];then 
  action="A" 
else 
  action="D" 
fi 
 
# get container ip by container name 
container_ip=`docker inspect -f '{{.NetworkSettings.IPAddress}}' $container_name 2> /dev/null` 
if [ -z $container_ip ];then 
  echo "[ERROR] Get container's (${container_name}) IP error, please ensure you have this container" 
  exit 2 
fi 
 
# parse arguments 
protocol_type=`echo $arguments | awk -F '/' '{print $2}'` 
if [ -z $protocol_type ];then 
  protocol_type="tcp" 
fi 
 
# check protocol 
if [ "$protocol_type"x != "tcp"x -a "$protocol_type"x != "udp"x ];then 
  echo "[ERROR] Only tcp or udp protocol is allowed" 
  exit 99 
fi 
 
machine_ip='' 
machine_port='' 
container_port='' 
# split the left arguments 
arguments=${arguments%/*} 
machine_ip=`echo $arguments | awk -F ':' '{print $1}'` 
machine_port=`echo $arguments | awk -F ':' '{print $2}'` 
container_port=`echo $arguments | awk -F ':' '{print $3}'` 
if [ -z $machine_port ];then 
  # arguments is: 234 
  container_port=$machine_ip 
  machine_port=$machine_ip 
  unset machine_ip 
elif [ -z $container_port ];then 
  # arguments is: 234:456 
  container_port=$machine_ip 
  machine_port=$machine_port 
  unset machine_ip 
fi 
 
# check port number function 
_check_port_number() { 
  local port_num=$1 
  if ! echo $port_num | egrep "^[0-9]+$" &> /dev/null;then 
    echo "[ERROR] Invalid port number $port_num" 
    exit 3 
  fi 
  if [ $port_num -gt 65535 -o $port_num -lt 1 ];then 
    echo "[ERROR] Port number $port_num is out of range(1-56635)" 
    exit 4 
  fi 
} 
 
# check port and ip address 
_check_port_number $container_port 
_check_port_number $machine_port 
 
if [ ! -z $machine_ip ];then 
  if ! echo $machine_ip | egrep "^${IPV4_RE}$" &> /dev/null;then 
    echo "[ERROR] Invalid Ip Adress $machine_ip" 
    exit 5 
  fi 
 
  # check which interface bind the IP 
  for interface in `ifconfig -s | sed -n '2,$p' | awk '{print $1}'`;do 
    interface_ip=`ifconfig $interface | awk '/inet addr/{print substr($2,6)}'` 
    if [ "$interface_ip"x == "$machine_ip"x ];then 
      interface_name=$interface 
      break 
    fi 
  done 
 
  if [ -z $interface_name ];then 
    echo "[ERROR] Can not find interface bind with $machine_ip" 
    exit 98 
  fi 
fi 
 
# run iptables command 
echo "[INFO] Now start to change rules to iptables" 
echo "[INFO] Changing POSTROUTING chain of nat table" 
iptables -t nat -${action} POSTROUTING -p ${protocol_type} --dport ${container_port} -s ${container_ip} -d ${container_ip} -j MASQUERADE 
if [ -z $interface_name ];then 
  echo "[INFO] Changing DOCKER chain of filter table" 
  iptables -${action} DOCKER ! -i docker0 -o docker0 -p ${protocol_type} --dport ${container_port} -d ${container_ip} -j ACCEPT 
  echo "[INFO] Changing DOCKER chain of nat table" 
  iptables -t nat -${action} DOCKER ! -i docker0 -p ${protocol_type} --dport ${machine_port} -j DNAT --to-destination ${container_ip}:${container_port} 
else 
  echo "[INFO] Changing DOCKER chain of filter table" 
  iptables -${action} DOCKER -i $interface_name -o docker0 -p ${protocol_type} --dport ${container_port} -d ${container_ip} -j ACCEPT 
  echo "[INFO] Changing DOCKER chain of nat table" 
  iptables -t nat -${action} DOCKER -i $interface_name -p ${protocol_type} --dport ${machine_port} -j DNAT --to-destination ${container_ip}:${container_port} 
fi 



感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • Dockerfile文本文件使用方法實例解析

    Dockerfile文本文件使用方法實例解析

    這篇文章主要介紹了Dockerfile文本文件使用方法實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • 在Window 10上安裝Docker圖文教程

    在Window 10上安裝Docker圖文教程

    Docker 是一個開源的應(yīng)用容器引擎,功能十分強大,相信開發(fā)者們都不陌生?,F(xiàn)在,docker已經(jīng)發(fā)布了支持Windows10的穩(wěn)定版本,對于用戶來說是一個福音。接下來,小編就詳細(xì)介紹Win10系統(tǒng)安裝docker教程。
    2018-03-03
  • 使用docker-compose搭建mysql主從詳細(xì)過程

    使用docker-compose搭建mysql主從詳細(xì)過程

    這篇文章主要給大家介紹了關(guān)于使用docker-compose搭建mysql主從的相關(guān)資料,Docker-Compose項目是Docker官方的開源項目,負(fù)責(zé)實現(xiàn)對Docker容器集群的快速編排,需要的朋友可以參考下
    2024-01-01
  • 如何優(yōu)化Docker鏡像的大小詳解

    如何優(yōu)化Docker鏡像的大小詳解

    本文詳細(xì)介紹了Docker的定義和優(yōu)點,以及如何通過優(yōu)化Docker鏡像來提高其輕量化和便攜性,通過使用輕量化基礎(chǔ)鏡像和多階段構(gòu)建,可以顯著減小Docker鏡像的大小,從而提高部署和運行效率
    2025-03-03
  • docker安裝drone的實現(xiàn)示例

    docker安裝drone的實現(xiàn)示例

    Drone是一款基于Docker的持續(xù)集成和持續(xù)部署平臺,可以幫助開發(fā)者自動化構(gòu)建、測試和部署應(yīng)用程序,本文主要介紹了docker安裝drone的實現(xiàn)示例,感興趣的可以了解一下
    2023-12-12
  • docker圖形化工具portainer詳解

    docker圖形化工具portainer詳解

    這篇文章主要介紹了docker圖形化工具portainer的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借價值,需要的朋友可以參考下
    2024-01-01
  • Rancher無法添加主機問題的解決方法

    Rancher無法添加主機問題的解決方法

    這篇文章主要給大家介紹了關(guān)于Rancher無法添加主機問題的解決方法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • 在docker中的mysql容器內(nèi)執(zhí)行命令與執(zhí)行SQL文件方式

    在docker中的mysql容器內(nèi)執(zhí)行命令與執(zhí)行SQL文件方式

    文章介紹了如何通過Docker進入MySQL容器執(zhí)行SQL文件,并總結(jié)了MySQL清空表數(shù)據(jù)的三種方法:TRUNCATE、DROP和DELETE,每種方法都有其適用場景和特點
    2025-01-01
  • 查看docker是否處于啟動狀態(tài)的方法詳解

    查看docker是否處于啟動狀態(tài)的方法詳解

    Docker 是一個開源的應(yīng)用容器引擎,讓開發(fā)者可以打包他們的應(yīng)用以及依賴包到一個可移植的容器中,本文主要給大家介紹了查看docker是否處于啟動狀態(tài)的方法,需要的朋友可以參考下
    2024-06-06
  • vscode中啟用docker擴展顯示無權(quán)限的問題解決

    vscode中啟用docker擴展顯示無權(quán)限的問題解決

    這篇文章主要介紹了如何解決vscode中啟用docker擴展顯示無權(quán)限的問題,并介紹允許VSCode進入Docker內(nèi)部進行調(diào)試的插件,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03

最新評論

汾阳市| 万宁市| 广饶县| 中山市| 高陵县| 改则县| 巴楚县| 龙州县| 涟水县| 澄江县| 池州市| 浪卡子县| 云浮市| 板桥市| 惠水县| 抚宁县| 通江县| 武清区| 崇仁县| 阿克苏市| 高青县| 阳东县| 正宁县| 大荔县| 柞水县| 淳安县| 冷水江市| 宝坻区| 姚安县| 屯昌县| 禄丰县| 九江市| 江山市| 民勤县| 宣武区| 乌拉特前旗| 丹东市| 开远市| 茌平县| 巴彦淖尔市| 元阳县|