Iptables – nginx docker container cannot see client ip when using ‘–iptables=false’ option

dockeriptables

I am using Docker 17.04.0-ce on Ubuntu 16.04.

I am running several containers on my host which expose public ports, some should be only accessible by certain ip ranges, while others, like an nginx proxy listening on port 80 and 443, should be publicly accessible.

Using the default configuration my iptables configuration of the INPUT chain was ignored, allowing all containers with bound ports to be accessed from anywhere. So I learned that I had to provide the --iptables=false option to docker, which worked fine.

While I now can control the access to the different ports using iptables, my nginx container is no longer able to see the ip address of the connecting client, but only gets the ip of the docker0 bridge (172.17.41.1 in my case).

Is there any way to allow the nginx container to see the connecting clients ip without loosing control over iptables again?

Side note: I do not want to put all containers on the host net (--net=host).

Best Answer

This depends on your IPTABLES setup. It sounds as if you masquerade while forwarding, something like this:

iptables -t nat -A POSTROUTING --out-interface docker0 -j MASQUERADE

If you want the client to see the original IP, you have to disable masquerading, and just use NAT and PREROUTING. There are tons of manuals for this out there.

Just make sure your containers use your host as default route, otherwise you'll end up not being able to answer because your responses don't originate from the correct ip.

To say more, you'd need to post your iptables configuration...