Docker on prem use AWS NAT to have fix IP address

  Kiến thức lập trình

I have connected my on prem with AWS via site to site. Everything works fine. I am already utilising AWS NAT instance in AWS with all of my networking and have one public IP address. Now I would like to utilise the same IP address for my on prem docker containers. I have read this article, which seem to explain how to do this. However still can’t make it work.

I have created a new network interface without masquerade:

docker network create --attachable --opt ‘com.docker.network.bridge.name=bridge-coi’ --opt ‘com.docker.network.bridge.enable_ip_masquerade=false’ bridge-coi

And created the rule:

sudo iptables -t nat -I POSTROUTING 1 -s 172.18.0.0/16 ! -o docker0 -j SNAT --to-source <PRIVATE IP OF NAT INSTANCE>

When i run:

sudo iptables -t nat -L POSTROUTING -n -v --line-numbers

I get:

1        2   112 MASQUERADE  0    --  *      !docker0  172.17.0.0/16        0.0.0.0/0           
2      101  6868 SNAT       0    --  *      !docker0  172.18.0.0/16        0.0.0.0/0            to:172.31.51.77
3        6   392 MASQUERADE  0    --  *      !br-8ed197dfb65b  172.18.0.0/16        0.0.0.0/0  

When i try to check my IP within the container:

docker run --rm --network bridge-coi byrnedo/alpine-curl http://www.myip.ch

I get:

curl: (6) Could not resolve host: www.myip.ch

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT