Search posts

Local-first PWA sync: last-writer-wins done carefully
Exactly-once doesn’t come for free: Kafka, Flink and ClickHouse
Building an end-to-end encrypted chat app with libsodium

Docker internal networks for egress, after my own guard broke backups

A home-made egress allow list raced container starts and broke the nightly backup. Docker internal networks, gw_priority and one nftables rule replaced it.

Written by
Published
Reading time
9 min
Written against
Docker Compose 5
Docker internal networks for egress, after my own guard broke backups

Most of the containers on my home server never need the internet: databases, monitoring, an object store. A compromised one could still reach anything, because a Docker bridge network routes out by default. How do you let only the containers that need it reach the internet, without a list you have to keep in sync? My first answer was a guard I wrote myself, and the night it started enforcing, the backup upload failed. The answer I kept is a feature Docker already had: internal networks.

This post is about the homelab that runs my side projects, set up as described in the post on self-hosting side projects. It covers how I first logged what the machines connect to, the guard I built and why it failed, the move to Docker's internal networks plus one nftables rule, and what this setup still does not control.

Watch before you block

Before blocking anything I wanted a list of who actually calls out. I wrote a small service, egresswatch, that reads the kernel's connection tracking (conntrack -E). Every connection to a public address becomes one JSON line when it opens, and another when it closes:

  • Who: the container that owns the source address (from docker inspect), or host for the machine itself.
  • Where: the address and port, plus the domain name from the DNS answers the machine saw for that address.
  • How much: bytes each way and duration, on close. This needs flow accounting turned on, nf_conntrack_acct, which is off by default.

An app cannot go around this: ignoring a proxy or resolving names itself still opens a connection. The limits are just as clear. It sees addresses, not URLs, since HTTPS is encrypted.

For the URL level, the apps report their own outgoing HTTP calls with OpenTelemetry auto-instrumentation: the Node agent with only the http and undici instrumentations on, the Java agent for the JVM apps. A collector keeps only client spans and strips query strings before logging. I tried Beyla first, since eBPF needs no change to the apps. To see the HTTPS calls of Node and Java it needed SYS_ADMIN and the host's PID namespace, which is root on the machine in effect. I removed it the same day.

My first guard: an allow list by container address

Less than an hour after egresswatch started logging, I wrote the guard. It had a list of compose services allowed out and an nftables table on the forward path. egresswatch filled nftables sets with those containers' current addresses, refreshed on every container start. It started in log mode, reporting "would block" without refusing anything. The log looked clean, and two minutes later I switched it to enforce.

That night's backup upload to object storage failed, and so did the weekly restore test. I found out from the backup alert, not from the guard.

Two ways it failed

The backup uploader was not a compose service. The backup script ran rclone with docker run, on Docker's default bridge. The guard's list named compose services, so rclone was not on it, and two minutes of log mode could never have caught it: the upload runs once a night. Log mode only proves something if it runs through a full cycle of every scheduled job, at least a day and a night here.

The guard had a race at container start. It allowed a container's address only after the container had started and egresswatch had seen the event. A container that connects in its first second, such as a short-lived uploader or a blackbox prober, had its first connections refused. That is a design flaw, not a bug to patch: any list keyed by runtime addresses has to be updated after the thing it describes already exists.

Sequence diagram with three participants: a container, egresswatch and the nftables guard. The container starts and connects in its first second, and the guard refuses it because its address is not in the set yet. egresswatch adds the address after it sees the start event, and later connections pass.ContaineregresswatchGuard(nftables)The container starts.connects in itsfirst secondrefused: notin the set yetadds its IP afterthe start eventlater connectionspassInternal network: no list,the rule exists before the container starts.
A list keyed by runtime addresses is always updated after the container already runs, so its first connections lose the race.

My first fix made it worse. I put rclone on the host's network, so it went around the guard rather than through it, and egresswatch now showed host instead of who was calling the object store. I reverted that within hours and made rclone a proper compose service on the list. By then the better design was clear.

Docker already has this: internal networks

docker network create --internal creates a network with no route out: containers on it reach each other, and nothing else. That is exactly the rule I wanted, enforced by Docker from the moment a container joins, with no list and no race.

The layout now:

Network role Internal Who joins it
Shared backend yes every stack
Databases yes the databases and only the services that use them
Object store yes the store and the proxy in front of it
Egress no only services that call out
LAN only no (one rule, below) what is reached over the LAN or the tailnet

A service that needs the internet joins the egress network in its own compose file. Since it is on several networks, it also needs to say which one is its default route. Compose has gw_priority for that (Compose 2.33.1 and later): the network with the highest value becomes the default gateway.

services:
  app:
    networks:
      backend: {}
      egress:
        gw_priority: 1

Who may leave the machine is now in the same file as the service, in code review, in place before the container starts. No address list exists, so nothing can fall out of sync.

The one rule Docker cannot express

Docker has internal networks (nothing out) and normal bridges (everything out), but nothing for "the LAN and the tailnet, not the internet". Prometheus and the log collector need exactly that: they scrape the other machine over Tailscale and should never reach a public address.

So one nftables table is left, matched by bridge name. Bridge names exist before any container joins, so this rule has no list and no race either. Docker names bridges br-<id> by default, so the script that creates the networks sets a fixed name through com.docker.network.bridge.name. Shortened, with the bridge name and log prefix changed:

table inet egress_guard {
  set private4 {
    type ipv4_addr; flags interval
    elements = { 10.0.0.0/8, 100.64.0.0/10, 127.0.0.0/8, 169.254.0.0/16,
                 172.16.0.0/12, 192.168.0.0/16 }
  }
  chain forward {
    type filter hook forward priority filter - 5; policy accept;
    iifname { "br-lanonly", "docker0" } jump private_only
  }
  chain private_only {
    ct state established,related accept
    ip daddr @private4 accept
    log prefix "blocked: " level warn
    counter reject with icmpx admin-prohibited
  }
}

100.64.0.0/10 is there because Tailscale gives out addresses from that range. docker0, the default bridge that a plain docker run uses, gets the same rule: anything started outside compose can reach the LAN but not the internet. That is what should have happened to rclone in the first place.

A refusal is logged and egresswatch turns it into an event with the container's name, so it reaches an alert. A container on an internal network has no route, so the app fails at once with "network unreachable". That error turned out to be easier to debug than a refused connection, because it shows up in the app's own log.

What changes when a network goes internal

  • No published ports. An internal network cannot publish ports to the host. My databases used to listen on 127.0.0.1 for maintenance. Now the host reaches a container by its address, or with docker exec.
  • Docker cannot convert or rename a network. Turning the existing shared network internal meant removing every container on it, creating the network again, and starting everything. My script for this stopped every stack for a few minutes.
  • A step that stops everything must bring everything back. The first run removed the tunnel container along with the rest, then decided which machine was live by looking for that same container. It was gone, so the step skipped the tunnel, and the sites returned errors for about 15 minutes instead of 3–5. The fix was to read "is this machine live" before removing anything. The lesson was to try a step like that on the dev VM first.
  • Every docker run in a script needs checking. The backup failure was one of these. Searching the repos for docker run before enforcing would have found it.

What this does not control

  • Where an allowed service goes. A container on the egress network can reach any public address. Limiting it to certain domains needs an egress proxy with an allow list, and that is a different, heavier design.
  • What happens inside an allowed connection. Watching connections tells you who talked to whom, not what was sent.
  • A compromised service that is allowed out. This only limits the blast radius of the containers that never needed the internet.

Summary

  • Log before you block, and keep a guard in log mode through a full cycle of every scheduled job. Reading conntrack shows who connects where, by container, but it sees addresses and not URLs.
  • A guard keyed by container addresses has a race at start and a list to keep in sync. Mine broke the nightly backup the first night it enforced.
  • Internal networks are Docker's own answer: no route out, enforced from the moment a container joins. Services that call out join an egress network, with gw_priority to make it their default route.
  • The one gap, "LAN but not internet", fits in a single nftables rule matched by bridge name: set a fixed bridge name, and include docker0.
  • Moving to internal networks means recreating containers. Test the step that stops everything on a spare machine first.
  • This does not limit where an allowed service goes. For that you need an egress proxy.

If you've restricted container egress another way, with a proxy, Cilium or something else, I'd love to compare notes. My email is on the home page.