How can i configure iptables policies with docker to block externals ip

I am trying to restrict access to containers from external IP. I followed docker documentation at https://docs.docker.com/network/iptables/ but it not working.

I have created a bridge network:

docker network create -d bridge --subnet 172.19.0.0/24 --opt com.docker.network.bridge.name=br-mynet mynet

Then I run container trought Docker-compose with previous network. Atfer than i can see iptables rules:

[email protected]:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (2 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.19.0.2           tcp dpt:postgresql
ACCEPT     tcp  --  anywhere             172.19.0.3           tcp dpt:omniorb
ACCEPT     tcp  --  anywhere             172.19.0.3           tcp dpt:8086
ACCEPT     tcp  --  anywhere             172.19.0.6           tcp dpt:https
ACCEPT     tcp  --  anywhere             172.19.0.7           tcp dpt:https
ACCEPT     tcp  --  anywhere             172.19.0.9           tcp dpt:8888
ACCEPT     tcp  --  anywhere             172.19.0.12          tcp dpt:https
ACCEPT     tcp  --  anywhere             172.19.0.8           tcp dpt:3000
ACCEPT     tcp  --  anywhere             172.19.0.5           tcp dpt:9092
ACCEPT     tcp  --  anywhere             172.19.0.4           tcp dpt:http-alt

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

At this step all external IP can connect to all host containers at 172.19.0.x. Then I apply docker rules as described in documentation to accept connection only from 10.223.20.173 :

iptables -I DOCKER-USER -i br-mynet ! -s 10.223.20.173 -j DROP

That would means the only external 10.223.20.173 can connect to containers. The iptables rules becomes:

[email protected]:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy DROP)
target     prot opt source               destination
DOCKER-USER  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-1  all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
DOCKER     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain DOCKER (2 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             172.19.0.2           tcp dpt:postgresql
ACCEPT     tcp  --  anywhere             172.19.0.3           tcp dpt:omniorb
ACCEPT     tcp  --  anywhere             172.19.0.3           tcp dpt:8086
ACCEPT     tcp  --  anywhere             172.19.0.6           tcp dpt:https
ACCEPT     tcp  --  anywhere             172.19.0.7           tcp dpt:https
ACCEPT     tcp  --  anywhere             172.19.0.9           tcp dpt:8888
ACCEPT     tcp  --  anywhere             172.19.0.12          tcp dpt:https
ACCEPT     tcp  --  anywhere             172.19.0.8           tcp dpt:3000
ACCEPT     tcp  --  anywhere             172.19.0.5           tcp dpt:9092
ACCEPT     tcp  --  anywhere             172.19.0.4           tcp dpt:http-alt

Chain DOCKER-ISOLATION-STAGE-1 (1 references)
target     prot opt source               destination
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
DOCKER-ISOLATION-STAGE-2  all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-ISOLATION-STAGE-2 (2 references)
target     prot opt source               destination
DROP       all  --  anywhere             anywhere
DROP       all  --  anywhere             anywhere
RETURN     all  --  anywhere             anywhere

Chain DOCKER-USER (1 references)
target     prot opt source               destination
DROP       all  -- !10.223.20.173        anywhere
RETURN     all  --  anywhere             anywhere

But with this configuration the ip 10.223.20.173 can’t connect to container.
On the over hand, when i apply the rules without the negative symbol !, that ip connect successful. But it should be the other way around.

Appreciate any help!

Source: StackOverflow