Docker ignores iptable rules when using “-p :

前端 未结 5 926
广开言路
广开言路 2021-01-31 22:10

Just realized a few days ago that Docker seems to bypass my iptable rules. I am not incredible experienced with Docker nor iptables. Tried a lot of different things the last day

5条回答
  •  鱼传尺愫
    2021-01-31 22:31

    To use iptables on published ports from docker containers, you need a combination of things:

    • DOCKER-USER table: docker uses this table for iptables rules that affect containers and is reserved specifically for user provided rules that won't be overwritten by the docker engine when it restarts.
    • conntrack: port forwarding can publish on one port and forward to another in the container. You can have multiple containers all listening on port 80 with different published ports on the host.

    To use these, the resulting iptables rule looks like:

    iptables -I DOCKER-USER -i eth0 -s 10.0.0.0/24 -p tcp \
      -m conntrack --ctorigdstport 8080 -j ACCEPT
    iptables -I DOCKER-USER -i eth0 ! -s 10.0.0.0/24 -p tcp \
      -m conntrack --ctorigdstport 8080 -j DROP
    

    This handles requests to the published port 8080/tcp (that's on the host, the container could be listening on 80 or any other port), and only accepts the requests from the 10.0.0.0/24 subnet. Everything outside of that subnet is dropped.

    Note that the DOCKER-USER table has a default rule to immediately return, so all changes should be inserted before that default rule in the table.

提交回复
热议问题