Docker – 从容器中为主机修改IPTABLES

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

我有一个问题,运行一个容器,这样的能力,它也可以修改主机的iptables。

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

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

问候。

默认情况下,Docker容器在隔离的networking名称空间内运行,在这个名称空间中,他们无法访问主机networkingconfiguration(包括iptables)。

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

 --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来运行。

--privileged标志不再需要。 从Docker 1.2开始,您现在可以使用参数--cap-add=NET_ADMIN--cap-add=NET_RAW来运行映像,这将允许内部iptables。

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

  • apt-get install iptables
  • 使用--net=host--cap-add=NET_ADMIN运行--net=host容器--cap-add=NET_RAW选项。

另外,如果你有一个缺lessiptables包的docker镜像,并且你不想从它创build一个自定义镜像,你可以在同一个networking空间里运行iptables容器。 例如,如果您有容器container-without-iptables正在运行,并且您想在同一个networking命名空间中启动一些container-with-iptables ,那么可以这样做:

 docker run -it --pid=container:container-without-iptables --net=container:container-without-iptables --cap-add sys_admin container-with-iptables