18

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!

14

Two things to bear in mind when working with docker's firewall rules:

  1. To avoid your rules being clobbered by docker, use the DOCKER-USER chain
  2. Docker does the port-mapping in the PREROUTING chain of the nat table. This happens before the filter rules, so --dest and --dport will see the internal IP and port of the container. To access the original destination, you can use -m conntrack --ctorigdstport.

For example:

iptables -A DOCKER-USER -i eth0 -s 8.8.8.8 -p tcp -m conntrack --ctorigdstport 3306 --ctdir ORIGINAL -j ACCEPT
iptables -A DOCKER-USER -i eth0 -s 4.4.4.4 -p tcp -m conntrack --ctorigdstport 3306 --ctdir ORIGINAL -j ACCEPT
iptables -A DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 3306 --ctdir ORIGINAL -j DROP

NOTE: Without --ctdir ORIGINAL, this would also match the reply packets coming back for a connection from the container to port 3306 on some other server, which is almost certainly not what you want! You don't strictly need this if like me your first rule is -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT, as that will deal with all the reply packets, but it would be safer to still use --ctdir ORIGINAL anyway.

  • Should this be edited to include the --ctdir? I use -m conntrack --ctstate NEW --ctorigdstport 3306 --ctdir ORIGINAL – lonix Sep 15 at 19:41
  • @Ionix, yes it should, although I've only just worked out why it's been confusing me. I've added a bit of explanation. – SystemParadox Sep 16 at 11:04
  • 1
    Note the default DOCKER-USER table contains the entry: -A DOCKER-USER -j RETURN which 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 at 18:29
  • @BMitch Or even better yet, add all rules in a new FILTERS chain, and -I insert new rules (like you said), to jump to it: -I INPUT -j FILTERS and -I DOCKER-USER -i eth0 -j FILTERS – lonix Sep 17 at 11:02
  • @BMitch However, I just checked my server, and the return rule is not there, maybe the latest docker version no longer inserts it? Good idea to use -I though, just to be safe. – lonix Sep 17 at 11:06
8

With Docker v.17.06 there is a new iptables chain called DOCKER-USER. This one is for your custom rules: https://docs.docker.com/network/iptables/

Unlike the chain DOCKER it is not reset on building/starting containers. So you could add these lines to your iptables config/script for provisioning the server even before installing docker and starting the containers:

-N DOCKER
-N DOCKER-ISOLATION
-N DOCKER-USER
-A DOCKER-ISOLATION -j RETURN
-A DOCKER-USER -i eth0 -p tcp -m tcp --dport 3306 -j DROP
-A DOCKER-USER -j RETURN

Now the port for MySQL is blocked from external access (eth0) even thought docker opens the port for the world. (These rules assume, your external interface is eth0.)

Eventually, you will have to clean up iptables restart the docker service first, if you messed it too much trying to lock down the port as I did.

  • I miss why this DOCKER-USER table is any different than any other user-added table.. It has no filter pre-applied to it so you still have to specify the interface names yourself. If you create a "MY-CHAIN" and insert it into the FORWARD chain it will have the same result, no? – ColinM Dec 4 '17 at 18:13
  • Yes, it makes a difference, because Docker inserts the DOCKER-USER chain into the FORWARD chain: -A FORWARD -j DOCKER-USER -A FORWARD -j DOCKER-ISOLATION That's why, the custom instructions are executed before the DOCKER chain. – ck1 Dec 5 '17 at 18:26
  • Note that if you use --dport inside DOCKER-USER this must match the internal IP of the container service, not the exposed port. These often match but not always and this could easily conflict with other services so I still argue that this DOCKER-USER solution is half-baked. – ColinM Dec 6 '17 at 22:03
4

UPDATE: While valid in 2015, this solution is no longer the right way to do it.

The answer seems to be in Docker's documentation at https://docs.docker.com/articles/networking/#the-world

Docker’s forward rules permit all external source IPs by default. To allow only a specific IP or network to access the containers, insert a negated rule at the top of the DOCKER filter chain. For example, to restrict external access such that only source IP 8.8.8.8 can access the containers, the following rule could be added: iptables -I DOCKER -i ext_if ! -s 8.8.8.8 -j DROP

What I ended up doing was:

iptables -I DOCKER -i eth0 -s 8.8.8.8 -p tcp --dport 3306 -j ACCEPT
iptables -I DOCKER -i eth0 -s 4.4.4.4 -p tcp --dport 3306 -j ACCEPT
iptables -I DOCKER 3 -i eth0 -p tcp --dport 3306 -j DROP

I didn't touch the --iptables or --icc options.

  • 1
    If you do iptables -vnL DOCKER, the destination ports are all ports within the container. If I get that right, that means the rules above would only affect the port 3306 within the container - that is, if you were to -p 12345:3306 your container, your rule would still be the one required to lock down the access (i.e. --dport 12345 wouldn't work), because the DOCKER chain's ACCEPT rules are post-NAT. – sunside Nov 6 '15 at 13:31
  • That's right, the rules need to relate to the ports within the containers. – GGGforce Nov 6 '15 at 13:35
  • 1
    Hum, that's sort of ugly if you happen to run multiple containers that use, say, an internal NGINX to do reverse proxying (e.g. Zabbix, a custom load balancer, etc.) because it would require you to know the container's IP in advance. I'm still searching to a solution for that problem that does not require --iptables=false, because this seems to be the worst choice of them all. – sunside Nov 6 '15 at 13:38
  • Thank you! You've solved my issue after many hours of searching. Now I'm finally able to jail MySQL just to my home IP address without exposing the soft underbelly to the entire world. – Matt Cavanagh Jul 16 '17 at 19:45
  • 1
    The DOCKER chain is not supposed to be directly manipulated by the user! Use the DOCKER-USER chain for that. Check the accepted answer. – Paul-Sebastian Manole Nov 8 '18 at 19:56
3

UPDATE: While this answer is still valid the answer by @SystemParadox using DOCKER-USER in combination with --ctorigdstport is better.

Here is a solution which persists well between restarts and allows you to affect the exposed port rather than the internal port.

iptables -t mangle -N DOCKER-mysql iptables -t mangle -A DOCKER-mysql -s 22.33.44.144/32 -j RETURN iptables -t mangle -A DOCKER-mysql -s 22.33.44.233/32 -j RETURN iptables -t mangle -A DOCKER-mysql -j DROP iptables -t mangle -A PREROUTING -i eth0 -p tcp -m tcp --dport 3306 -j DOCKER-mysql

I've built a Docker image that uses this method to automatically manage the iptables for you, using either environment variables or dynamically with etcd (or both):

https://hub.docker.com/r/colinmollenhour/confd-firewall/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.