Egress firewalling: stopping a compromised container from phoning home
Inbound rules get all the attention; outbound is where a breach actually gets expensive

Contents
Every firewall tutorial I read for the first several years of running a homelab was about keeping things out. Block inbound except the ports you actually need, put a reverse proxy in front, done. It took an actual incident - a container running outdated software that got compromised through a dependency vulnerability, and then spent several hours quietly trying to reach an external address before I noticed - to make me realise the inbound rules had done their job perfectly and it hadn’t mattered, because nothing was stopping the compromised container from calling out once it was already inside.
That’s the gap egress filtering closes, and it’s a gap almost every home network has by default, because outbound traffic is normally allowed on principle: your devices need to reach the internet, so most default configurations simply don’t restrict outbound at all, on the assumption that anything already inside the network is trusted. That assumption is exactly what a compromise breaks, and it’s worth treating “already inside” and “trusted” as separate facts rather than one.
Why outbound matters more than it looks like it should
An attacker who compromises a service rarely wants to sit inside it doing nothing - the value is in exfiltrating data, downloading a second-stage payload, or turning the box into a relay for further attacks, and every one of those actions requires an outbound connection the compromised process initiates itself. A container serving a web application has no legitimate reason to open a connection to an arbitrary external IP on an arbitrary port, and if it does, that’s either a misconfiguration or a compromise - there’s no innocent third explanation, which makes egress traffic from a well-understood service one of the highest-signal things you can monitor and restrict.

Inbound rules protect the front door. Egress rules protect the fact that a breach, once it happens, stays contained rather than becoming a pivot point or a slow bleed of data heading somewhere you’ll never notice through inbound monitoring alone, because inbound rules were never watching that direction in the first place.
Default-deny outbound, and why it’s the right posture per container
The single most effective change is inverting the default from “allow outbound unless blocked” to “deny outbound unless explicitly allowed,” applied at the container or service level rather than the whole network, because a blanket network-wide default-deny breaks normal browsing and updates for every device that isn’t the thing you’re actually trying to contain. Docker’s default bridge network doesn’t restrict egress by default at all - a container can reach anywhere the host can reach unless something explicitly stops it - which is convenient for getting a stack running and exactly the gap an attacker benefits from once something inside that stack is compromised.
nftables rules scoped to a container’s specific IP or its Docker network subnet are the most direct way to apply this without extra tooling:
| |
That last drop line is the entire point - it’s a default-deny for that specific container’s traffic, with only the database connection and DNS explicitly carved out. Anything else the container tries to reach, a command-and-control server, an exfiltration endpoint, a cryptominer’s pool, hits that drop rule and goes nowhere, and the counter keyword means you can watch the drop count climb if something starts trying.
Doing it with Docker’s own network isolation instead
For a Compose-based stack, the more maintainable pattern than hand-written nftables rules is Docker’s own internal networks, which don’t have a route to the outside world at all unless a container is also attached to an external-facing network:
| |
internal: true on the db-internal network means anything attached only to it - the database in this case - has no route out to the internet at all, regardless of what’s inside the container or what it tries to do. It can talk to app because they share that network, and it can’t talk to anything else, full stop, which is the correct posture for a database that has no legitimate reason to make an outbound connection to anywhere. This is a much lower-maintenance approach than manual firewall rules because it’s declared once in the Compose file and enforced by Docker’s own network driver rather than a separate ruleset you have to keep in sync as containers come and go.
The web-facing app container still needs outbound access for things like fetching updates or calling a legitimate external API, so it stays on the regular frontend network - the goal is matching each container’s egress posture to what it actually, legitimately needs to reach, and defaulting to nothing for anything that doesn’t have a stated reason.
DNS as the exfiltration channel nobody blocks
The most commonly overlooked egress channel is DNS itself. Even a network with reasonably tight outbound rules on ports 80 and 443 often leaves port 53 wide open, because blocking DNS breaks basically everything, and that’s exactly what makes DNS such an effective covert channel - data can be encoded into subdomain labels and exfiltrated one query at a time to a domain the attacker controls, and it looks like ordinary DNS traffic to anything not specifically inspecting query patterns. DNS sinkholing at the network level catches known-bad destinations, but a genuinely novel exfiltration domain won’t be on any blocklist yet.
The more robust fix is forcing every container to resolve DNS through a specific internal resolver you control - the db-internal example above already does this implicitly, since a container with no route out can’t reach an external DNS server even if it tried - and logging query volume and entropy from anything that shouldn’t be generating much DNS traffic at all. A database container suddenly issuing hundreds of DNS queries a minute to unfamiliar domains is a loud signal precisely because it should be issuing close to zero.
Watching what tries to leave, not just blocking it
A static ruleset tells you what’s allowed and denied, but it doesn’t tell you what actually happened at the moment something tried to break the rule, and that gap matters for incident response - a dropped connection in a firewall log is a fact, but it’s missing the process, the parent process, and the sequence of events that led to it. eBPF-based tools like Falco or Cilium’s network policy layer sit at the kernel level and can attribute an outbound connection attempt to the specific process and even the specific file that made it, which turns “something in this container tried to reach an unknown address” into “this specific binary, loaded from this specific path, made this specific call” - the difference between a firewall log entry and an actual incident timeline.
| |
This is genuinely more setup than a firewall rule and it’s not something to reach for on a first pass at egress control - get the default-deny network posture working first, since that’s what actually stops the traffic. The process-level visibility is what you add once the basic containment is in place and you want to know not just that something was blocked, but exactly what tried and why, which matters considerably more when the answer to “was this a real compromise or a misconfigured update checker” needs to be fast and confident rather than a guess.
The same principle applies past containers
Everything above is framed around Docker because that’s where the gap is most commonly left wide open by default, but the same reasoning applies to any host running a mixed workload, container or not. A VPS running a handful of services benefits from the exact same posture applied at the host firewall level - default-deny outbound, explicit allowances per service for exactly the destinations it needs, DNS routed through a resolver you control. Cloud security groups and their on-prem equivalents support egress rules as a first-class concept precisely because this isn’t a container-specific problem, it’s a “what does this specific thing actually need to talk to” problem that happens to be easiest to demonstrate with containers because their network boundaries are already so clearly drawn.
Containers make a good starting point precisely because their expected outbound behaviour is usually far more predictable than a general-purpose server’s - a database container has an enumerable, short list of legitimate destinations, where a developer’s daily-driver VM might reach dozens of services in a normal week. Start with the workloads where “everything this needs to reach” fits in a short list, and the effort-to-value ratio is at its best.
Troubleshooting: what breaks when you turn this on
The most common casualty of enabling default-deny egress is a container that turns out to have a legitimate outbound dependency nobody remembered - a license check phoning a vendor’s server, an update checker, a metrics or telemetry call baked into the image that isn’t documented anywhere obvious. These fail silently in a way that’s often harder to diagnose than a total outage, because the core service keeps working while some background feature quietly stops, and the first sign is sometimes weeks later when an update mechanism turns out to have been broken the whole time.
Rolling out egress rules with logging before enforcement - nftables’s log statement on the drop rule, or a permissive Docker network run in a staging environment first - surfaces these dependencies before they become confusing production mysteries. A week of logged-but-not-dropped traffic against a new ruleset, reviewed once, catches the overwhelming majority of legitimate connections you’d otherwise have blocked blind, and it’s a far better use of that week than debugging a mysteriously broken update mechanism three months from now with no memory of having changed anything.
The second common issue is a container needing to reach another service by hostname rather than IP, which fails if the egress rule is written against a specific IP that changes on container restart - Docker doesn’t guarantee a container keeps the same internal IP across recreations unless it’s pinned. Scoping rules to the Docker network’s subnet rather than an individual container IP, or better, using Docker’s own network isolation as the primary control and reserving hand-written firewall rules for cases that genuinely need finer granularity, avoids most of this churn.
Is it worth the setup effort
For any container handling something you’d actually mind losing - a database, an authentication service, anything holding credentials or personal data - egress filtering is one of the higher-value security investments in a homelab relative to the effort involved, because it directly limits the blast radius of the exact failure mode inbound rules were never designed to catch: something already inside, trying to get data or instructions out. It works alongside patching and access control as the layer that determines whether a compromise that does happen stays contained to one component or turns into an active, ongoing leak nobody notices until the bill or the breach notification arrives.
I default every new container to a network with no outbound route now, and add specific allowances only when something actually needs them. It’s more setup than the alternative, and it’s caught more than one misbehaving container quietly trying to reach somewhere it had no business reaching.




