r/docker • u/TopLychee1081 • 3d ago
Network isolation
When multiple Dockerised apps require access to the same database container, perhaps Traefik or another reverse proxy, and need to have metrics scraped by Prometheus, how can one possibly maintain network isolation? We could create different networks for each required connection, which ends up being close to the cartesian product of user apps, and databases, proxies, and Prometheus. That approach pretty soon runs out of available IPs under default Docker and becomes completely unmanageable. It seems like the only workable solution ends up being having a common network for all containers. Is there a better way to manage this?
5
u/herkalurk 3d ago
Why do you need to isolate each individual container?
If you have a group of containers which requires the same resources, couldn't you just put those in a virtualized Network and the others in other networks?
2
u/zolij86 3d ago
I think what you need here is multiple user defined networks targeting specific containers.
1
u/Only-Stable3973 3d ago edited 3d ago
Are trying to build a monitoring-stack? In my main monitoring-stack since Prometheus is connected to the needed networks and I use Traefik labels they will be scrapped.
1
u/MasterChiefmas 3d ago
If I understand what you are asking for, I think what you want to look into is configuring Inter-Container Communication on Docker. If you disable it(it's enabled by default), I think it does what you want- prevents containers on the same subnet from being able to talk to each other without explicitly allowing it. i.e. you should be able to connect them all to the same virtual network, but they wouldn't be able to see each other. So it's like client isolation in WiFi, or port isolation on a switch.
1
u/NoDevelopment206 1d ago
you may not need a network per pair at all. one way is to split by role like a few networks only, say proxy_net, db_net and metrics_net. each app joins just the ones it needs, and the database only sits on db_net so nothing else can reach it. the proxy and prometheus join the extra networks they need to talk to. that gives you maybe 4 or 5 networks total instead of a cartesian product. you can also mark db_net as internal so it has no outside access at all.
1
u/jjangg96 2d ago
the ip exhaustion part is fixable on its own. docker hands out a /16 per network from a pretty small default pool, so you hit "all predefined address pools have been fully subnetted" after around 30 networks. set default-address-pools in /etc/docker/daemon.json, something like base 10.10.0.0/16 with size 24, restart the daemon, and you get 256 networks of 254 hosts each. widen the base if you need more.
for the layout, one network per app scales a lot better than one per pair. each app sits on its own network, and the shared stuff (proxy, the db, prometheus) joins each of those networks, since a container can be on several at once. the apps still can't see each other because they never share a network. so it ends up as roughly one network per app plus whatever the shared containers attach to.
1
u/NoDevelopment206 2d ago
one thing that helps with the ip worry is that you can shrink the default address pool in daemon.json so each network gets a small subnet. also you could put prometheus on its own metrics network and just attach each app to it, instead of one network per pair. that way it grows by number of apps, not apps times databases. are the databases shared by all the apps or does each app get its own?
0
u/jaysprenkle 1d ago
You beat the cartesian product problem by using canonicalization.
https://en.wikipedia.org/wiki/Canonicalization
It's widely used in Node-Red, BizTalk, Kestra, etc.
Periodically publish a document in a canonical form for data you wish to monitor to a message passing system. Anything that wants to be aware of that data can subscribe. You get everything completely decoupled as a bonus.
As an example. You get a purchase order file. You publish it in *a standard format* to mqtt, msmq, aws, or service bus. The inventory subsystem subscribes and updates inventory levels. The general ledger system subscribes and updates the ledger. The delivery system subscribes and asks your robots to start packaging the goods into boxes. Your monitoring system subscribes and updates the box on your desk showing how many sales you had.