---
title: "Docker Port Publishing and NAT, Explained Rule by Rule"
date: "2026-11-19T08:15:00+07:00"
description: "Follow a published port from PREROUTING to DNAT to MASQUERADE and the filter table, plus the docker-proxy, hairpin NAT, and how to read the real iptables output."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/docker-port-publishing-and-nat-explained.jpg"
alt: "iptables NAT flow diagram showing DNAT and MASQUERADE rules for a published Docker port"
tags: ['docker', 'networking', 'iptables', 'nat', 'containers']
---

`docker run -d -p 8080:80 nginx` is one flag, but it writes about a dozen iptables rules and starts a userspace proxy process. When publishing "doesn't work" — the port answers from the host but not from the LAN, or vice versa — the cause is always one of those rules. Docker's port publishing is NAT: **DNAT** rewrites the destination on the way in, **MASQUERADE** rewrites the source on the way out, and a filter chain decides what's allowed. Here is the actual packet path, with real output from an Engine 29.7.2 host.

## Where the rules come from

Publish a port and inspect the nat table:

```bash
docker run -d --name web -p 8080:80 nginx:alpine
sudo iptables -t nat -S
```

```text
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.29.0.0/16 ! -o br-1f2e3d4c5b6a -j MASQUERADE
-A DOCKER -p tcp -m tcp --dport 8080 -j DNAT --to-destination 172.29.0.2:80
```

Read them as stages of a packet's journey:

1. **`PREROUTING` / `OUTPUT` → `DOCKER`:** packets addressed to *this host* (matched by `addrtype --dst-type LOCAL`) are diverted to Docker's chain. The `OUTPUT` copy handles traffic originating on the host itself — this is why `curl localhost:8080` works, not just remote clients.
2. **`DOCKER` chain → DNAT:** `--dport 8080` is rewritten to `172.29.0.2:80`. From here the packet is routed toward the container's bridge.
3. **`POSTROUTING` → MASQUERADE:** the container's source address (`172.29.0.0/16`) is replaced with the host's outgoing interface address whenever the packet leaves via a non-bridge interface — so replies find their way back.

The filter table finishes the job:

```bash
sudo iptables -t filter -S DOCKER
```

```text
-A DOCKER -d 172.29.0.2/32 ! -i br-1f2e3d4c5b6a -o br-1f2e3d4c5b6a -p tcp -m tcp --dport 80 -j ACCEPT
-A DOCKER ! -i br-1f2e3d4c5b6a -o br-1f2e3d4c5b6a -j DROP
```

The first rule admits exactly one flow: inbound TCP to port 80 on the container. The second **drops everything else** heading out of that bridge — Docker's isolation between containers and the outside world, implemented as a default-deny filter. That pairing (per-port ACCEPT, blanket DROP) is why a published port is open and an unpublished one is silently dropped, not merely unroutable.

## The other half: the docker-proxy

iptables isn't the whole story. Look at the host's listeners:

```bash
ss -ltnp | grep -E '8080|8082'
```

```text
LISTEN 0  4096  0.0.0.0:8080   0.0.0.0:*  users:(("docker-proxy",pid=1234,...))
LISTEN 0  4096  127.0.0.1:8082 0.0.0.0:*  users:(("docker-proxy",pid=1240,...))
```

Docker starts a small userspace proxy in addition to the NAT rules. The daemon flag governing it is documented as `--userland-proxy`: *"Use userland proxy for loopback traffic (default true)"*. The proxy's job is the case NAT handles worst — traffic to `127.0.0.1`, which never traverses `PREROUTING`. With the proxy, `curl localhost:8080` works out of the box; turning it off (e.g. `--userland-proxy=false`) pushes loopback traffic onto a NAT-based workaround instead.

Two practical consequences:

- **Binding is part of publishing.** `-p 8080:80` binds `0.0.0.0:8080`; `-p 127.0.0.1:8082:80` binds only loopback. The listener you see in `ss` *is* the firewall-free entry point for local processes.
- **Localhost publishing had a real bug.** Binding a published port to `127.0.0.1` could fail to work before Engine 28.0.0 (moby/moby#45610). If loopback publishing behaves oddly on an old daemon, upgrade before blaming your app.

## Hairpin NAT: reaching a container through the host it lives on

The nastiest publishing scenario is a container calling its own published host port — traffic must leave and come back. Docker handles it with a dedicated masquerade rule:

```text
-A POSTROUTING -s 172.29.0.2/32 -d 172.29.0.2/32 -p tcp -m tcp --dport 80 -j MASQUERADE
```

Source and destination are both the container's IP: the packet to the host address gets its source rewritten so the reply can return through the NAT path instead of being routed back to the container directly (which would break the connection's symmetry). This is classic hairpin NAT, and it's why "container reaches host-published port of its own service" works without extra configuration. Note the scope: it's written per published port (`--dport 80`), and only exists while the container is running.

Docker Desktop and rootless Docker run their own forwarding layers rather than relying solely on these rules — the mental model is the same, but don't expect byte-identical output there.

## EXPOSE vs. publishing: a common mix-up

`EXPOSE 80` in a Dockerfile (or `expose:` in Compose) does **not** open anything. It's metadata — it shows up in `docker port`/inspect and lets you publish with `-P` (capital) to map all exposed ports. Only `-p host:container` writes the DNAT rule. A container can be `EXPOSE 80` and completely unreachable, and that's not a bug.

Quick verification of what's actually live:

```bash
docker port web
```

```text
80/tcp -> 0.0.0.0:8080
```

An empty response here means no `-p` was ever applied — the DNAT rule doesn't exist, and no amount of firewall debugging will change that.

## Symptom → missing rule

When a published port misbehaves, this table maps what you observe to the layer that owns it:

| Symptom | Likely cause | Check |
|---|---|---|
| Nothing listening on host port | Never published (or container stopped) | `docker port web` |
| Works on the host, not from the LAN | Bound to loopback only (`-p 127.0.0.1:...`) or host firewall | `ss -ltnp`, `ufw`/`firewalld` |
| LAN works, `localhost` doesn't (old daemon) | Loopback publishing bug before 28.0.0 | Docker version |
| Works until the container restarts | DNAT rule points at a stale IP | `iptables -t nat -S DOCKER` |
| Everything fine, then dropped | A custom iptables script flushed Docker's chains | `iptables -t filter -S DOCKER` |

## The bottom line

Port publishing is four moving parts: a DNAT rule rewriting destination port to container address, a MASQUERADE rule fixing the return path, a filter chain that accepts exactly your port and drops the rest, and the docker-proxy listening on the host so loopback works. When a published port misbehaves, identify which part is missing — no listener (`docker port` empty), no DNAT (old or removed container), no ACCEPT rule (custom `iptables` flushing Docker's chains), or a firewall in front — and the mystery evaporates.

## Further reading

- [Port mapping fundamentals: host vs. container ports](https://blog.redlinesoft.net/posts/port-mapping-fundamentals-host-container)
- [Binding ports to specific host interfaces and IPs](https://blog.redlinesoft.net/posts/binding-ports-host-interfaces-ips)
- [Docker docs: Port publishing](https://docs.docker.com/engine/network/port-publishing/)
- [Docker docs: Firewall and iptables](https://docs.docker.com/engine/network/firewall-iptables/)
- [Docker docs: Packet filtering and firewalls](https://docs.docker.com/engine/network/packet-filtering-firewalls/)
- [Docker docs: dockerd reference (--userland-proxy)](https://docs.docker.com/reference/cli/dockerd/)
