1、将当前iptables的配置写入保存到/etc/sysconfig/iptables

2、保存

/etc/init.d/iptables sava

3、修改iptables配置(vi /etc/sysconfig/iptables):

在适当位置增加下面红色的三行,然后重启iptables即可。(30612 是容器对外提供服务的端口)

-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER -d 172.17.0.10/ ! -i docker0 -o docker0 -p tcp -m tcp --dport -j ACCEPT
-A DOCKER -d 172.17.0.117/ ! -i docker0 -o docker0 -p tcp -m tcp --dport -j ACCEPT
-A DOCKER -d 172.17.0.7/ ! -i docker0 -o docker0 -p tcp -m tcp --dport -j ACCEPT
-A DOCKER -d 172.17.0.7/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 30612 -j ACCEPT
COMMIT
# Completed on Thu Mar  ::
# Generated by iptables-save v1.4.7 on Thu Mar  ::
*nat
:PREROUTING ACCEPT [:]
:POSTROUTING ACCEPT [:]
:OUTPUT ACCEPT [:]
:DOCKER - [:]
-A PREROUTING -p tcp -m tcp --dport -j DNAT --to-destination 172.17.0.7:
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/ ! -o docker0 -j MASQUERADE
-A POSTROUTING -s 172.17.0.10/ -d 172.17.0.10/ -p tcp -m tcp --dport -j MASQUERADE
-A POSTROUTING -s 172.17.0.117/ -d 172.17.0.117/ -p tcp -m tcp --dport -j MASQUERADE
-A POSTROUTING -s 172.17.0.7/ -d 172.17.0.7/ -p tcp -m tcp --dport -j MASQUERADE
-A POSTROUTING -s 172.17.0.7/32 -d 172.17.0.7/32 -p tcp -m tcp --dport 30612 -j MASQUERADE
-A OUTPUT ! -d 127.0.0.0/ -m addrtype --dst-type LOCAL -j DOCKER
-A DOCKER ! -i docker0 -p tcp -m tcp --dport -j DNAT --to-destination 172.17.0.10:
-A DOCKER ! -i docker0 -p tcp -m tcp --dport -j DNAT --to-destination 172.17.0.117:
-A DOCKER ! -i docker0 -p tcp -m tcp --dport -j DNAT --to-destination 172.17.0.7:
-A DOCKER ! -i docker0 -p tcp -m tcp --dport 30612 -j DNAT --to-destination 172.17.0.7:30612
COMMIT
# Completed on Thu Mar  ::

最后:

重启iptables服务

# centos6.x
service iptables restart # centos7.x
systemctl restart iptables.service

使用iptables为docker容器动态添加端口映射的更多相关文章

  1. 给一个正在运行的Docker容器动态添加Volume

    给一个正在运行的Docker容器动态添加Volume本文转自:http://dockone.io/article/149 [编者的话]之前有人问我Docker容器启动之后还能否再挂载卷,考虑到mnt命 ...

  2. docker 笔记--运行中的容器如何添加端口映射

    解决: iptables -t nat -A DOCKER -p tcp --dport ${YOURPORT_1} -j DNAT --to-destination ${CONTAINERIP}:$ ...

  3. Centos7 docker容器启动后添加端口映射

    docker容器启动后添加端口映射的两种方法: 一.通过修改防火墙策略添加端口映射 docker容器已创建好,但是想在容器内配置tomcat监控,需要新的端口去访问,但是映射时没有映射多余端口,此时, ...

  4. docker容器启动后添加端口映射

    DOCKER 给运行中的容器添加映射端口 方法1 1.获得容器IP 将container_name 换成实际环境中的容器名 docker inspect `container_name` | grep ...

  5. 重启服务器后Docker容器暴露的端口外网突然访问不了!!

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  6. Docker动态添加端口,不需要重新建立镜像

    Docker容器在运行期间有时可能会需要修改或者添加暴露的端口,但是有时候运行的容器又不想再另外建立一个新的镜像.这时可以找到docker容器的存放地方,然后直接修改配置文件. 我们的容器都是保存在/ ...

  7. 通过iptables限制docker容器端口

    如何限制docker暴露的对外访问端口 docker 会在iptables上加上自己的转发规则,如果直接在input链上限制端口是没有效果的.这就需要限制docker的转发链上的DOCKER表. # ...

  8. Docker实战(五)之端口映射与容器互联

    除了网络访问外,Docker还提供了两个很方便的功能来满足服务访问的基本需求:一个是允许映射容器内应用的服务端口到本地宿主主机;另一个是互联机制实现多个容器间通过容器名来快速访问. 1.端口映射实现访 ...

  9. 解决docker容器启动时候无法映射端口的问题

    当我们停止防火墙后,docker容器启动映射端口可能无法映射端口,这个时候需要重建docker0网桥. 详细的错误是这样的: docker: Error response from daemon: d ...

随机推荐

  1. LeetCode--链表

    1.使用常量空间复杂度在O(n log n)时间内对链表进行排序. 思路: 因为题目要求复杂度为O(nlogn),故可以考虑归并排序的思想. 归并排序的一般步骤为: 1)将待排序数组(链表)取中点并一 ...

  2. sh make.sh fatal error: opencv2/opencv.hpp: No such file or directory

    问题: sh make.sh fatal error: opencv2/opencv.hpp: No such file or directory 解决: sudo apt-get install l ...

  3. ArrayList 和 LinkedList 的区别是什么?(未完成)

    ArrayList 和 LinkedList 的区别是什么?(未完成)

  4. Python 之 PyMySQL 安装和使用

    Python具有内置的SQLite支持. 在本节中,我们将学习使用MySQL的相关概念和知识. 在早期Python版本一般都使用MySQLdb模块,但这个MySQL的流行接口与Python 3不兼容. ...

  5. springboot的简单了解与使用

    1. Spring Boot 1.1. 什么是Spring Boot 1.2. Spring Boot的优缺点 1.3. 快速入门 1.3.1. 设置spring boot的parent <pa ...

  6. The "web.xml" is called web application deployment descriptor

    3.3  Configure the Application Deployment Descriptor - "web.xml" A web user invokes a serv ...

  7. Lua 学习之基础篇八<Lua 元表(Metatabble)&&继承>

    讲到元表,先看一段table的合并动作. t1 = {1,2} t2 = {3,4} t3 = t1 + t2 attempt to perform arithmetic on a table val ...

  8. 2 使用unitest 模块扩展功能测试

    准备做一个 待办事项清单网站,来演示 Web 开发过程中的所有主要步骤.以及如何在各个步骤中运用TDD理念. ”功能测试“: 从用户的角度查看应用是如何运作的. 从某种程度上可以作为应用的说明书. 作 ...

  9. sql时间加减

    /时间转成年月日时分秒select date_format(now(),'%Y%m%d%H%i%S')//时间转成年月日select date_format(now(),'%Y%m%d')//去年此时 ...

  10. 024_统计/var/log 有多少个文件,并显示这些文件名

    #!/bin/bash#使用 ls 递归显示所有,再判断是否为文件,如果是文件则计数器加 1cd /var/logsum=0for i in `ls -r *`do     if [ -f $i ]; ...