Docker - 从容器修改主机的IPTABLES

Mac*_*ski 7 iptables docker fail2ban

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

我有一个问题是运行具有这种功能的容器,它也可以修改主机iptables.

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

赋予容器标志特权只允许我控制iptables此容器.有没有办法iptables通过容器控制主机?

问候.

Dmi*_*san 13

--privileged不再需要标志。与码头工人开始1.2现在可以带参数运行你的形象--cap-add=NET_ADMIN--cap-add=NET_RAW这将使内部的iptables。

可能还值得注意的是,在官方 Ubuntu 映像iptables中未安装Docker Hub包。所以一般指令应该是

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

此外,如果您有一个缺少iptables包的 docker 镜像,并且您不想从中创建自定义镜像,您可以iptables在同一网络空间中运行容器。例如,如果您有容器container-without-iptables正在运行,并且您想container-with-iptables在同一个网络命名空间中启动一些容器,您可以执行以下操作:

docker run -it --pid=container:container-without-iptables --net=container:container-without-iptables --cap-add sys_admin container-with-iptables
Run Code Online (Sandbox Code Playgroud)


lar*_*sks 11

默认情况下,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.
Run Code Online (Sandbox Code Playgroud)

您将需要同时运行--privileged--net=host.