Lesson 11 of 19 · 22m
Published Ports, iptables, and Your Firewall's Blind Spot
-p publishes on 0.0.0.0 through NAT chains your host firewall never evaluates — loopback binds and the DOCKER-USER chain are the actual controls.
An operator sets ufw deny 6379, runs docker run -d -p 6379:6379 redis, and checks the box: Redis is protected. It is not. Redis is reachable from the internet and stays that way until somebody understands what -p actually configures — because it is not a firewall rule, and the firewall rule was never consulted.
What -p actually installs
docker run -p 6379:6379 asks the daemon to program DNAT and filter rules in Docker's own iptables chains — DOCKER in the nat table, DOCKER-USER in filter. Inbound packets get destination-rewritten and forwarded to the container. That is the mechanism that matters: forwarded traffic traverses FORWARD, not INPUT, and ufw/firewalld rules live almost entirely in INPUT. Your deny rule is real; it is evaluated on a path the packet never takes. This is the single most misdiagnosed exposure in Docker deployments — the GitHub issue about it has been open since 2014, and the "but my firewall says deny" reports have never stopped.
Docker does honor one user-controlled chain: DOCKER-USER exists specifically for your rules and survives daemon restarts:
iptables -I DOCKER-USER -i eth0 -p tcp --dport 6379 -j DROPThe second mechanism is the default bind. -p 6379:6379 is shorthand for 0.0.0.0:6379:6379 — every interface, including the public one. -p 127.0.0.1:6379:6379 binds loopback only. Compose ports: has identical semantics, which is how dev databases with password app end up on the office LAN.
The fix landscape, in order of sanity: bind to 127.0.0.1 for anything not meant to be public; put user rules in DOCKER-USER; front real services with a reverse proxy and publish nothing else; or run the daemon with "iptables": false, which disables Docker's NAT and filter rules entirely — a working choice only if you intend to manage container networking yourself.
The evidence is a decade of breach data. Shodan has catalogued exposed Redis, MongoDB, and Elasticsearch instances for years, and the 2020 "meow" attacks enumerated and wiped thousands of them — a good share of which were containers published on 0.0.0.0 behind host firewalls their operators believed were filtering.
Container-to-container traffic is a different path
Published-port rules govern host-to-container. Container-to-container is its own world: attach services to a user-defined bridge network and Docker gives you service-name DNS — db and api resolve on the compose network, which is why DATABASE_URL points at db rather than localhost. The legacy default bridge network does not get automatic name resolution; that behavior only exists on user-defined networks. Nothing about -p limits what containers can say to each other — isolation between containers is a network-topology question, not a publishing question. Multiple bridges exist precisely so services that should not talk cannot.
Two extremes worth knowing: --network none gives a container no interfaces beyond loopback, for workloads that should never see a packet. --network host drops namespacing entirely so the container shares the host's interfaces — useful for wire-level tooling, useless for isolation, and port collisions are now your problem.
In Compose, expose: declares a port reachable only from other services on the network; ports: publishes to the host. Confusing the two is how "internal only" services get an internet audience.
Try this
On a VM you own: ufw deny 6379, then docker run -d -p 6379:6379 redis:7, and redis-cli -h <public-ip> ping from another machine. It answers. Republish bound to 127.0.0.1: and watch it stop answering. Then list what Docker installed: iptables -t nat -L DOCKER -n and iptables -L DOCKER-USER -n.
Field notes
- Audit
docker ps --format '{{.Ports}}'on production hosts; published ports are public API surface, not an implementation detail. - Docker publishes on IPv4 and IPv6 both — a
127.0.0.1:bind covers v4; check v6 separately. - firewalld hits the same blind spot; Docker's chains sit ahead of yours either way.
- Docker also runs a
docker-proxyprocess per published port — visible inss -lntnp— for paths iptables cannot express (IPv6, hairpin NAT). It is one more reason-pexposure escapes firewall reasoning. -p 6379:6379binds::too on dual-stack hosts; the loopback idiom for v6 is::1:."ip": "127.0.0.1"indaemon.jsonchanges the default bind address host-wide — one line that retires the whole footgun class.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
What is Kubernetes? Container Orchestration at Scale
Kubernetes runs your Docker containers across a cluster of machines, handles scheduling, health checking, rolling deployments, auto-scaling, and self-healing. When your application outgrows a single server, Kubernetes is how it runs on ten.
What is Docker? Containers, Images, and Why Every Developer Needs to Know This
Docker packages your application and everything it needs to run into a container that behaves identically on your laptop, your teammate's machine, and the production server. "It works on my machine" stops being a problem. Here is how it actually works and what production usage looks like.