I don’t know exactly because I haven’t done it but I am pretty sure you cannot do it with only docker compose. In this case you have Docker Swarm, which complicates things more. You can try to configure macvlan networks for your containers but that can cause other issues or you can try to use iptables to change the routing on the host. I am not good at iptables so I can’t give you the final solution.
That is for incoming traffic on a specific host interface. The outgoing traffic still goes on the default route, and if 172.16.0.1
is the ip address of the router (which probably the case) and not the host, Docker will not be able to forward port from that IP.