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
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]