My goal is to limit access to docker containers to just a few public IP addresses. Is there a simple, repeatable process to accomplish my goal? Understanding only the basics of iptables while using Docker's default options, I'm finding it very difficult.
I'd like to run a container, make it visible to the public Internet, but only allow connections from select hosts. I would expect to set a default INPUT policy of REJECT and then only allow connections from my hosts. But Docker's NAT rules and chains get in the way and my INPUT rules are ignored.
Can somebody provide an example of how to accomplish my goal given the following assumptions?
- Host public IP 80.80.80.80 on eth0
- Host private IP 192.168.1.10 on eth1
docker run -d -p 3306:3306 mysql- Block all connection to host/container 3306 except from hosts 4.4.4.4 and 8.8.8.8
I'm happy to bind the container to only the local ip address but would need instructions on how to set up the iptables forwarding rules properly which survive docker process and host restarts.
Thanks!
--ctdir? I use-m conntrack --ctstate NEW --ctorigdstport 3306 --ctdir ORIGINAL– lonix Sep 15 '19 at 19:41DOCKER-USERtable contains the entry:-A DOCKER-USER -j RETURNwhich will run before the above if you use-A. A solution is to insert rules at the head in reverse order with-I. – BMitch Sep 16 '19 at 18:29FILTERSchain, and-Iinsert new rules (like you said), to jump to it:-I INPUT -j FILTERSand-I DOCKER-USER -i eth0 -j FILTERS– lonix Sep 17 '19 at 11:02-Ithough, just to be safe. – lonix Sep 17 '19 at 11:06-I. – BMitch Sep 17 '19 at 13:06--ctorigdstport? Then I can open a few ports (placing a few rules before this one) if necessary and then have everything else blocked. – Alexis Wilke Feb 21 '20 at 01:54iptables -I DOCKER-USER -i eth0 -j DROP. All the incoming traffic to my docker host generates on eth0, so a bottom rule like this worked well as far as I know. – RicHincapie Sep 30 '21 at 15:45