Docker - 从容器修改主机的 IPTABLES

Docker - modifying IPTABLES for host from container

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

我在 运行 具有这样功能的容器时遇到问题,它还可以修改主机 iptables。

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

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

此致。

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

如果你想让你的容器能够修改主机的网络配置,你需要将--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。

可能还值得注意的是,官方 Ubuntu 图片来自 Docker Hub iptables 包未安装。 所以一般说明应该是

  • apt-get install iptables
  • 运行 docker 带有 --net=host--cap-add=NET_ADMIN --cap-add=NET_RAW 选项的容器。

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

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