docker的iptables策略详解和用户自定义策略的添加方式

 更新时间:2024年10月10日 11:06:46   作者:玄德公笔记  
在Docker环境下,直接修改iptables以允许特定主机访问指定端口时,需要考虑Docker自身的iptables规则,Docker通过修改nat表的PREROUTING链和filter表的FORWARD链来处理外部对Docker容器的访问,绕过了filter表的INPUT链

1. 需求

需求:

iptables增加策略,允许指定主机访问本机的指定端口,但是该端口是docker容器提供的服务。

2. 分析

不想了解原理,直接操作的可以跳过本节

2.1 缘起

  • 如果不是docker,我们可以这样写:
iptables -I INPUT -p tcp --dport 80 -j DROP
iptables -I INPUT -s 10.10.181.198 -p tcp --dport 80 -j ACCEPT
  • 但是docker建立了自己的iptables规则,将绕过filter表的INPUT链,接下来我们分析docker的iptables规则:

2.2 docker的iptables规则

  • 但是对于docker,访问则绕过了filter表的INPUT链
  • 而是通

注意:但是本机访问docker服务或容器间互访,依然通过的是filter表的INPUT链

1)nat表

查看iptables的nat表,内容如下:

[root@liubei-test nginx01]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
DOCKER     all  --  anywhere             anywhere             ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
DOCKER     all  --  anywhere            !loopback/8           ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
MASQUERADE  all  --  172.17.0.0/16        anywhere
MASQUERADE  all  --  172.20.0.0/16        anywhere
MASQUERADE  all  --  172.19.0.0/16        anywhere
MASQUERADE  all  --  172.29.0.0/16        anywhere
MASQUERADE  all  --  192.168.176.0/20     anywhere
MASQUERADE  tcp  --  192.168.176.2        192.168.176.2        tcp dpt:netopia-vo2
MASQUERADE  tcp  --  172.29.0.2           172.29.0.2           tcp dpt:20090
MASQUERADE  tcp  --  172.29.0.2           172.29.0.2           tcp dpt:10090
MASQUERADE  tcp  --  172.29.0.2           172.29.0.2           tcp dpt:lrp
MASQUERADE  tcp  --  172.20.0.2           172.20.0.2           tcp dpt:http
MASQUERADE  tcp  --  172.19.0.2           172.19.0.2           tcp dpt:http

Chain DOCKER (2 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere
DNAT       tcp  --  anywhere             anywhere             tcp dpt:http to:172.20.0.2:80

1.Chain PREROUTING 将请求转发到DOCKER链处理:

DOCKER all -- anywhere anywhere ADDRTYPE match dst-type LOCAL
  • ADDRTYPE:iptables的一个扩展模块,用于根据地址类型进行匹配。
  • dst-type LOCAL:表示目标地址必须是本地地址

2.Chain DOCKER 修改了目标地址:

DNAT tcp -- anywhere anywhere tcp dpt:http to:172.20.0.2:80

2)filter表

[root@liubei-test src]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  10.10.87.18          anywhere             tcp dpt:2375
DROP       tcp  --  anywhere             anywhere             tcp dpt:2375

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (4 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.18.0.2           tcp dpt:http

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

1.因为nat表修改了访问的目标地址,因此不再由filter表的INPUT链处理,而是交给了filter表的FORWARD链处理

2.FORWARD链会将请求依次交给如下链处理

注意的是,iptables的规则是匹配到即跳出。

DOCKER-USER

  • 作用:允许用户在此自定义规则

Chain DOCKER-ISOLATION-STAGE-1

  • 选择交给Chain DOCKER-ISOLATION-STAGE-2 处理
  • 作用:主要用于实现Docker容器之间的网络隔离

DOCKER

  • docker自动创建的iptables规则

3. 操作

如上文,我们只需修改预留给我们的filter表的DOCKER-USER链即可

iptables -I DOCKER-USER -p tcp --dport 80 -j DROP
iptables -I DOCKER-USER -s 10.10.181.201 -p tcp --dport 80 -j ACCEPT

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • docker配置skywalking 监控springcloud应用的详细步骤

    docker配置skywalking 监控springcloud应用的详细步骤

    本文分步骤给大家讲解docker配置skywalking 监控springcloud应用的方法,感兴趣的朋友一起看看吧
    2025-04-04
  • docker mysql容器如何开启慢查询日志

    docker mysql容器如何开启慢查询日志

    本文主要介绍了docker mysql容器如何开启慢查询日志,文中根据实例编码详细介绍的十分详尽,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • 一文详解如何获取docker镜像中的jar包

    一文详解如何获取docker镜像中的jar包

    本人在工作中遇到了需要在docker查看jar这种操作,查看了许多资料,终于还是找到了,下面这篇文章主要给大家介绍了关于如何获取docker镜像中jar包的相关资料,需要的朋友可以参考下
    2024-05-05
  • 阿里云CentOS 6.5 安装Docker详细步骤

    阿里云CentOS 6.5 安装Docker详细步骤

    这篇文章主要介绍了阿里云CentOS 6.5 安装Docker的相关资料,现在都是云时代和大数据时代,希望大家也能跟着时代的步伐,需要的朋友可以参考下
    2016-10-10
  • Docker安装部署neo4j的实战过程

    Docker安装部署neo4j的实战过程

    Docker是一种容器化技术,可以在Linux系统上部署应用程序,下面这篇文章主要给大家介绍了关于Docker安装部署neo4j的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-05-05
  • 如何查看docker run启动参数命令(推荐)

    如何查看docker run启动参数命令(推荐)

    这篇文章主要介绍了如何查看docker run启动参数命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • 如何在docker中安装seata

    如何在docker中安装seata

    文章介绍了如何在Docker中安装Seata,包括下载镜像、运行容器、配置数据库、修改配置文件、启动Seata以及在微服务项目中使用Seata的步骤,感兴趣的朋友跟随小编一起看看吧
    2025-11-11
  • docker部署gitlab实践

    docker部署gitlab实践

    这篇文章主要介绍了docker部署gitlab实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2026-03-03
  • 树莓派安装Docker的方法步骤

    树莓派安装Docker的方法步骤

    这篇文章主要介绍了树莓派安装Docker的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • 关于docker清理Overlay2占用磁盘空间的问题(亲测有效)

    关于docker清理Overlay2占用磁盘空间的问题(亲测有效)

    使用Docker过程中,长时间运行服务容器,导致不能进行上传文件等操作,今天通过本文给大家详细介绍下docker清理Overlay2占用磁盘空间的相关知识,感兴趣的朋友一起看看吧
    2022-03-03

最新评论