r/docker • • 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?

4 Upvotes

30 comments sorted by

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?

-8

u/TopLychee1081 3d ago

You can't figure out why network isolation would be preferred?

3

u/herkalurk 3d ago

You clearly need communication between some of your containers. Trying to fully isolate each one and yet have them communicate seems like a futile effort.

-8

u/TopLychee1081 3d ago

2

u/herkalurk 3d ago

There is practical security, and there is unreasonable security. You have control of these containers, so you don't have to treat them as inherently untrusted devices like transient guest wireless networks. You have a clear use case where they do need to communicate with each other.

0

u/TopLychee1081 3d ago

And the purpose of my post is to try to find that optimal balance of security and maintainability. There are likely some design patterns or Docker features that I'm unaware of that might give me something closer to the best of both worlds; optimal security without a lot of administrative pain. And as for trust; I never assume third party code is secure; vulnerabilities exist whether by accident, or by malicious intent. Limiting the attack surface available just makes sense.

2

u/herkalurk 3d ago

And that's fine. By docker standards though you should just have an individual database instance for each app that needs it and not use a shared database container.

The simplest solution would be to create docker compose files for each stack of an app and database and then when you bring up that compose docker will dynamically create a network for you isolating those containers. Unless you specify the network information inside the compose file, you will get a disposable Network tied only to that deployment.

0

u/TopLychee1081 3d ago

That can be done for databases, though it makes a lot of things more difficult. And it can't be done for the likes of a reverse proxy as you can only have a single lidtrner on a port. Also doesn't work if you want a single instance of Prometheus scraping metrics from multiple containers. You always end up with one of two poor alternatives; proliferation of networks as each network only has two nodes, or shared networks where it's not desirable to have some of the nodes talking to each other. I have ICC to investigate; that might be the missing piece of the puzzle.

2

u/herkalurk 2d ago

Containerized applications have piles of security holes. We have bunches of kubernetes apps and clusters at my job and they have to use entire /23 networks as a source in rules because you can't know which kubernetes worker will host the container at a given time.

1

u/SevaraB 16h ago

Docker containers are processes, not servers. In a Docker network, the network IS the IPC.

Frankly, place your containers better and just use more Docker networks. If two services aren’t part of the same service set and don’t need east/west connectivity, they shouldn’t be on the same network in the first place- zero trust 101: don’t put anything that doesn’t need to be there inside the same east/west network.

2

u/zolij86 3d ago

I think what you need here is multiple user defined networks targeting specific containers.

0

u/TopLychee1081 3d ago

The problem is the sheer number of networks that would need to be defined.

5

u/zolij86 3d ago

Well, if you want network isolation, you need isolated networks..

1

u/SevaraB 16h ago

And? Templatizing that is exactly why Kubernetes and helm charts exist.

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.

2

u/TopLychee1081 3d ago

Not specifically. The monitoring is simply for observability over all the apps.

1

u/Only-Stable3973 2d ago

If you are looking for a stack to monitor things this is what I am using you can use it as an example and take what you like.

services:
  # Prometheus - Metrics collection and storage
  prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: unless-stopped
    volumes:
      # Map the parent config directory; traefik_pass.txt will look inside it natively
      - ./prometheus/config:/etc/prometheus
      - prometheus_data:/prometheus
      - /run/udev/data:/run/udev/data:ro
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=30d'
      - '--web.enable-lifecycle'
    ports:
      - "9090:9090"
    env_file:
      - ./prometheus/.env
    networks:
      - monitoring # docker-monitoring network
      - proxy # traefik reverse proxy network
      - servarrnetwork # arr-stack and wireguard network

  # Grafana - Visualization and dashboards
  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    restart: unless-stopped
    environment:
      - GF_INSTALL_PLUGINS=grafana-piechart-panel,grafana-clock-panel
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning:/etc/grafana/provisioning
    ports:
      - "3000:3000"
    env_file:
      - ./grafana/.env
    networks:
      - monitoring # docker-monitoring network
      - proxy # traefik reverse proxy network - Allows routing your Grafana metrics panels through Traefik Reverse Proxy 

  # cAdvisor - Container metrics
  cadvisor:
    # Swapped to the official GitHub registry multi-arch modern release
    image: ghcr.io/google/cadvisor:v0.61.1
    container_name: cadvisor
    restart: unless-stopped
    privileged: true
    devices:
      - /dev/kmsg
    volumes:
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
      - /dev/disk/:/dev/disk:ro
    ports:
      - "8081:8080"
    env_file:
      - ./cadvisor/.env
    networks:
      - monitoring # docker-monitoring network

  # Node Exporter - Host system metrics
  node-exporter:
    image: prom/node-exporter:latest
    container_name: node-exporter
    restart: unless-stopped
    command:
      - '--path.rootfs=/rootfs'
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      # Excludes scanning heavy automation folders (media, torrents, etc.)
      - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc|usr/lib/wsl|mnt/c|mnt/wsl|Docker|data|media|downloads)($$|/)'
      - '--collector.filesystem.fs-types-exclude=^(9p|tmpfs|autofs|binfmt_misc|cgroup|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|mqueue|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|sysfs|tracefs)$'
      - '--no-collector.softnet'
      - '--no-collector.netstat'
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
      - /run/udev/data:/run/udev/data:ro
    ports:
      - "9100:9100"
    env_file:
      - ./node-exporter/.env
    networks:
      - monitoring # docker-monitoring network

  # Dozzle - Docker log viewer
  dozzle:
    image: amir20/dozzle:latest
    container_name: dozzle
    restart: unless-stopped
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    ports:
      - "8888:8080"
    env_file:
      - ./dozzle/.env
    networks:
      - monitoring # docker-monitoring network

networks:
  monitoring:
    name: monitoring # docker-monitoring network
    driver: bridge
  servarrnetwork: # arr-stack and wireguard network
    external: true
  proxy: # traefik reverse proxy network
    external: true

volumes:
  prometheus_data:
  grafana_data:

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.

0

u/TopLychee1081 3d ago

Sounds like it might be what I'm looking for. An important consideration is keeping containers as orthogonal, encapsulated, isolated, and independent as possible, and keep everything as simple and maintainable as possible.

1

u/MasterChiefmas 3d ago edited 3d ago

keep everything as simple and maintainable as possible

Your design sounds like it might end up limiting how simple you can keep it if the ICC doesn't manage what you need. If I understand what you are trying to do, you are basically trying to implement point to point connections between a lot of containers. There's no way I can think of that doesn't have a lot of setup and management somewhere no matter where it's implemented.

Maybe a 3 tier network, where the middle tier is basically proxying/gatekeeping the outer tiers. So apps on one side, proxies in the middle, and shared services on the other side. The proxy serviceswould be attached to both networks in addition to their own, so they can see either side, but the sides aren't able to see each other directly. ICC would prevent things on either side from communicating directly with each other, and the sides don't have a direct network path to each other. But really, it's upping network complexity to gain the isolation. I think you need to describe in more detail what the end points are, and what things need to be able to talk where. Are you doing multiple database services? Can they talk to each other? A shared database? Proxying that might difficult. Databases are often a common point, so they tend to need to be able to touch everything. Maybe if you have an API in the middle that all the client endpoints hit and isolation can be assisted at the data level. Again, we're back to complexity that's fundamental to what you are wanting to do.

The issue here is more that you are a single individual/team implementing it. Really, this is basically how the Internet is deployed, but no single entity is responsible for all the parts like you are in this scenario.

1

u/TopLychee1081 3d ago

I'll look at ICC and see if that can give me what I need. If it allows me to restrict such that only outbound connections are allowed by app containers, and inbound allowed by say a database container, then a single network can be used for database connectivity without apps being able to communicate between themselves. That would keep it pretty simple. A network for each db container, one for proxy, one for prometheus (with the reverse of the above being true; ie; one container outbound, all others inbound), and possibly another for Crowdsec. Appreciate you giving me an avenue to explore.

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/TopLychee1081 1d ago

The issue is that all the apps requiring, say Traefik access can talk to each other if they're on the same network.

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.

1

u/TopLychee1081 1d ago

Can you explain how this is applied to Docker networking?