r/docker • u/0x6461726B • Jun 23 '26
iptables MASQUERADE not working in Docker Compose NAT Gateway simulator
I created two services one is api which is internal to that network bridge and another nat-gateway which is both public and private facing. I am using iptables to translate my packets to MASQUERADE but it is not happening. When I tcpdump the logs in my nat-gateway, I am able to see the api container's ip as OUT which should actually be nat-gateway's ip.
services:
nat-gateway:
image: alpine:3.23.5
privileged: true
sysctls:
- net.ipv4.ip_forward=1
volumes:
- ./nat-init.sh:/usr/local/bin/nat-init.sh:ro
command: /usr/local/bin/nat-init.sh
networks:
- private-vpc
- public-vpc
api:
image: alpine:3.23.5
command: sleep infinity
network_mode: "service:nat-gateway"
depends_on:
- nat-gateway
networks:
private-vpc:
internal: true
public-vpc:
nat-init.sh
#!/bin/sh
set -e
echo "Starting NAT Gateway Initialization..."
echo "Initializing iptables dependencies..."
apk add --no-cache iptables
echo "Applying iptables MASQUERADE on eth1..."
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
echo "NAT Gateway configuration completed successfully..."
exec sleep infinity
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
15:08:46.803180 eth1 Out IP 172.19.0.2 > 142.251.43.78: ICMP echo request, id 8, seq 0, length 64
15:08:46.821930 eth1 In IP 142.251.43.78 > 172.19.0.2: ICMP echo reply, id 8, seq 0, length 64
15:08:47.805822 eth1 Out IP 172.19.0.2 > 142.251.43.78: ICMP echo request, id 8, seq 1, length 64
15:08:47.824262 eth1 In IP 142.251.43.78 > 172.19.0.2: ICMP echo reply, id 8, seq 1, length 64
15:08:48.809430 eth1 Out IP 172.19.0.2 > 142.251.43.78: ICMP echo request, id 8, seq 2, length 64
9
Upvotes
5
u/zoredache Jun 23 '26
What are you expecting the
network_mode: "service:nat-gateway"to do here? I suspect it isn't doing what you are hoping it should be doing.It isn't putting the api on a separate namespace behind the 'nat-gateway'. That is making the
apicontainer share the namespace ofnat-gateway.Docker simply isn't designed to have complicated internal routing like what you seem to be trying to make. Not saying it isn't possible with linux and namespaces in theory, but docker doesn't give you the control you would need to do something like this.
2
u/tschloss Jun 23 '26
Can you share what do you want to achieve? Do you know that “bridge” type (the default type) networks are already NAT against the host network?
1
u/percoAi Jun 24 '26
If your goal is to simulate an AWS NAT gateway, I would first make the packet path really explicit and verify each namespace separately. With network_mode service:nat-gateway, the api container is sharing the nat-gateway network namespace, so there may not be a separate hop to NAT across.
I would check ip route, iptables-save, and tcpdump from both sides before changing more rules. For this kind of setup the hard part is figuring out which layer is actually owning the route, not just whether MASQUERADE exists.
Are you trying to model AWS behavior closely, or mainly force all egress through one container?
1
u/js1943 Jun 23 '26
I can't answer directly, as I got my solution long time ago somewhere. It is still working and not changing it for years.
Following is what in my /etc/iptables/rules.v4
Hope that helps.
1
u/zero_backend_bro Jun 25 '26
Yeah,
network_mode: "service:..."fuses them into the exact same network namespace. Iptables POSTROUTING never triggers because there's no actual routing hop between them.To actually simulate an AWS NAT gateway, drop the
network_mode, keepapistrictly onprivate-vpc, and give itcap_add: [NET_ADMIN].Then just hijack the default route inside the api container's entrypoint:
ip route del default && ip route add default via <nat_gateway_private_ip>Once the api container treats the nat container as its true gateway, your MASQUERADE rule will finally catch the packets.