Disclosure up front: I run IPYE, a company that leases IP space. This is a practical how-to, and IPYE comes up where it helps, with a short call to action at the end.

Running each scraping job or customer workload in its own container is tidy. Giving each container its own outbound address from your range makes it traceable: abuse reports, blocks and logs map straight to a container. Docker doesn't do this out of the box, because by default all containers masquerade behind the host's primary address. This guide uses one Docker network per egress address, plus SNAT rules, with addresses from a /24 from IPYE. The examples use the documentation prefix 198.51.100.0/24, with egress addresses 198.51.100.21 to 198.51.100.23 on the host.

Put the egress addresses on the host

for i in 21 22 23; do ip addr add 198.51.100.$i/32 dev eth0; done 

If the /24 is routed to the host, add them to lo instead, and make sure the default route can carry traffic from them.

One Docker network per egress address

Docker's bridge driver supports a host-binding option for outbound masquerading on recent versions, com.docker.network.host_ipv4:

docker network create --subnet 172.30.21.0/24 \ -o com.docker.network.host_ipv4=198.51.100.21 job21 docker network create --subnet 172.30.22.0/24 \ -o com.docker.network.host_ipv4=198.51.100.22 job22 

With this option, Docker SNATs that network's outbound traffic to the given address, not the host's default. Check that your Docker version supports it (it appeared around Docker 20.10/23.0). If it doesn't, use the nftables approach below.

The nftables approach (any version)

Create the networks with masquerading disabled:

docker network create --subnet 172.30.23.0/24 \ -o com.docker.network.bridge.enable_ip_masquerade=false \ -o com.docker.network.bridge.name=br-job23 job23 

Then add your own SNAT rule:

table ip egress_nat { chain post { type nat hook postrouting priority srcnat; policy accept; ip saddr 172.30.23.0/24 oifname "eth0" snat to 198.51.100.23 } } 

Load it with nft -f, and make it persistent. Because masquerading is off for that network, Docker won't add a conflicting rule, and your SNAT decides the source address.

Run containers

docker run -d --name crawler-21 --network job21 example/crawler docker run -d --name crawler-23 --network job23 example/crawler 

Verify

docker exec crawler-21 curl -s https://ifconfig.co # 198.51.100.21 docker exec crawler-23 curl -s https://ifconfig.co # 198.51.100.23 

If you see the host's main address, either Docker's masquerade rule is matching first (check nft list ruleset for Docker's chains), or the host option isn't supported.

Egress filtering per container network

Block SMTP and limit ports per network in the forward path:

table inet egress_filter { chain fwd { type filter hook forward priority 0; policy accept; ip saddr 172.30.0.0/16 tcp dport 25 drop ip saddr 172.30.0.0/16 tcp dport != { 80, 443 } ct state new drop } } 

Docker manages its own DOCKER-USER chain for user rules when it uses iptables. With nftables-based rules in a separate table, the drop still applies, because a drop verdict in any table is final, while an accept in one table doesn't override a drop in another.

Logging

Record which container ran on which network, and when. A start/stop event log from docker events works. Combined with the fixed mapping from network to egress address, it answers "who used 198.51.100.22 at 14:00?"

Reverse DNS

Give each egress address a PTR record that names the job class (crawler-22.bot.example.net), with a matching forward record.

Getting the addresses

A leased /24 from IPYE gives you enough egress addresses for many container networks, with reverse DNS, a LOA and a ROA.

Next step

Set up the range in the IPYE portal, create one Docker network per egress address, and verify each container's outbound IP.

About IPYE

IPYE leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS, and ASN and region changes apply automatically. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. More at ipye.com. Questions: ipye@turnel.com.

Disclosure: I run IPYE