欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

docker 动态映射运行的container端口实例详解

程序员文章站 2024-02-09 14:09:10
docker动态映射运行的container端口,最近做项目,对于docker动态映射运行的container端口的资料有必要记录下,以便以后在用到, docker自...

docker动态映射运行的container端口,最近做项目,对于docker动态映射运行的container端口的资料有必要记录下,以便以后在用到,

docker自带了expose命令,可以通过编写dockerfile加-p参数方便的映射container内部端口,但是对于已经运行的container,如果你想对外开放一个新的端口,只能编辑dockerfile然后重新build,有点不太方便。

其实docker本身使用了iptables来做端口映射的,所以我们可以通过一些简单的操作来实现动态映射运行中的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 



我们要做的就是根据自己container的情况配置iptables规则。

首先是filter这个表,我们要配置其放通转发,docker默认已经将所有的forward规则放到了docker这个自建链(chain)中了,这样方便配置,也方便查看。

然后再配置nat表为其配置dnat,这个才是端口转发的核心配置。这个表中只需要配置postrouting和docker链即可,这里不讲为什么这么配置,如果想要深入了解iptables请google一下。

下面举个例子:

假如我有一个container,名字为nginx(通过运行docker ps命令即可查询),现在我在docker内部运行nginx程序,监听了8888端口,我希望外网可以通过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


通过外网访问curl http://ip:8899就会显示nginx下配置的html页面

最后iptables规则

[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 

当然,使用手动配置也是比较麻烦的,所以我写了一个脚本来自动配置端口映射,使用方法脚本中有说明

#!/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 



感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!