As @dpg points out, this problem is frustating if you need to tackle it from a newbie point of view.
The main problem for me (as I try to resolve also the problems of @dpg's answer), is that the Docker documentation is confusing in two of the pages that address this (link1 and link2)
To summarize, and to save time for others, if you don't have a lot of knowledge, and fall into the "Docker and iptables", the answer is there, just that they have missed this: where ext_if is the name of the interface providing external connectivity to the host.
Instead, in the "Understand container communication" link, there is indeed a little text that exactly points that ext_if should be the network interface.
So, for me to limit the access to a docker exposed port (ex: 6782) (that means that the DOCKER-USER needs to be modified and not the common INPUT chain) to a certain IP (ex: 192.27.27.90) and restrict all others, I need to do this, which works in my case:
sudo iptables -I DOCKER-USER -p tcp -i eth0 ! -s 192.27.27.90 --dport 6782 -j REJECT
(Here I suppose that the network interface that communicates with the outside world is eth0
and that you want to REJECT
instead of DROP
).
If more clarification is needed, I will be glad to assist.