首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker -从容器修改主机的IPTABLES

问Docker -从容器修改主机的IPTABLES
EN

Stack Overflow用户
提问于 2015-05-11 21:30:19
回答 2查看 11.5K关注 0票数 8

我想运行一个带有中央日志和fail2ban服务的docker容器,以防止dos/ddos攻击。

我在运行一个容器时遇到了一个问题,这个容器的功能可能还会修改主机的iptables。

有一个项目ianblenke/docker-fail2ban,但是它不工作...

赋予容器标志特权只允许我控制这个容器上的iptables。有没有办法通过容器控制主机iptables?

致以问候。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-11 21:55:11

默认情况下,Docker容器在隔离的网络命名空间内运行,在该命名空间中,它们无权访问主机网络配置(包括iptables)。

如果您希望您的容器能够修改主机的网络配置,则需要将--net=host选项传递给docker run。从docker-run(1)手册页:

代码语言:javascript
复制
--net="bridge"
   Set the Network mode for the container
       'bridge': creates a new network stack for the container on the docker bridge
       'none': no networking for this container
       'container:': reuses another container network stack
       'host':  use  the host network stack inside the container.
       Note: the host mode gives the container full access to
       local system services such as D-bus and is therefore
       considered insecure.

您需要同时使用--privileged和--net=host运行。

票数 14
EN

Stack Overflow用户

发布于 2017-06-13 22:04:29

不再需要--privileged标志。从Docker1.2开始,您现在可以使用参数--cap-add=NET_ADMIN和--cap-add=NET_RAW运行您的镜像,这将允许内部iptables。

同样值得注意的是,在Docker Hub的官方Ubuntu镜像中,没有安装iptables包。所以一般的指令应该是

带有--net=host和--cap-add=NET_ADMIN --cap-add=NET_RAW选项的

  • apt-get install iptables
  • run docker容器。

此外,如果您有一个缺少iptables包的docker镜像,并且您不想从它创建自定义镜像,那么您可以在同一网络空间中运行带有iptables的容器。例如,如果您正在运行容器container-without-iptables,并且您想要在同一网络名称空间中启动一些container-with-iptables,您可以这样做:

代码语言:javascript
复制
docker run -it --pid=container:container-without-iptables --net=container:container-without-iptables --cap-add sys_admin container-with-iptables
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30169013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档