Use it when a containerised service is not reachable from outside.
Published Ports and Port Mapping
Publishing ports maps a host port to a container port (-p 8080:80), implemented with NAT rules by Docker or hostPort/NodePort/LoadBalancer in Kubernetes.
1. docker port <c> — mapping.
2. Host listening: ss -ltnp | grep <port>.
3. Inside: app listens on 0.0.0.0:<port>?
4. Firewall/DOCKER-USER.
5. Test curl http://<host>:<port>.
client → host port → NAT → container port → app
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Test from the host with curl http://127.0.0.1:<port>, then from another machine. If the host test works but the remote does not, check cloud security groups and host firewall. If neither works, check the app's listen address.
Step 2 Common failures Narrow the fault
An app bound to 127.0.0.1 inside the container is unreachable even with the port published. UFW rules do not block published ports unless DOCKER-USER is used. Two containers cannot publish the same host port.
The app must listen on 0.0.0.0 inside. Published ports may bypass host firewall rules (use DOCKER-USER). Binding to 127.0.0.1:8080:80 limits exposure to localhost.
docker ps -a— containers and exit codesdocker logs <c>— container outputdocker inspect <c>— config, mounts, network, state
ReferenceWhat it is1 section · depth on demand
How it works
Docker adds DNAT rules in the DOCKER chain and, for local connections, may run docker-proxy. Traffic reaches the container's veth on the bridge. Rules in the DOCKER-USER chain are the supported place to filter published ports.
ReferenceImportant terms & boundaries3 sections · depth on demand
Publishing safely
docker run -p 127.0.0.1:8080:80 nginx # only localhost
docker run -p 8443:443 nginx # all host interfaces
docker port <container>
Published ports bypass UFW/firewalld rules because Docker inserts its own iptables rules; filter in the DOCKER-USER chain or bind to specific IPs. In Kubernetes use Services, not hostPort.
In depth
Publishing a port (-p host:container) creates a DNAT rule from the host address and port to the container IP and port. Without publishing, a container on a bridge network is reachable only from the host and from containers on the same network.
Address forms
| Flag | Listens on |
|---|---|
-p 8080:80 | All host addresses, IPv4 and IPv6 |
-p 127.0.0.1:8080:80 | Loopback only |
-p 192.0.2.10:8080:80 | One host address |
-p 8080:80/udp | UDP instead of TCP |
-P | Every EXPOSEd port to a random high port |
Firewall interaction
Docker inserts its rules ahead of most host firewall rules, so a UFW or firewalld rule that "blocks" port 8080 does not stop a published container port. Filter published ports in the DOCKER-USER chain, or publish on 127.0.0.1 and put a reverse proxy in front.
iptables -I DOCKER-USER -i eth0 ! -s 192.0.2.0/24 -p tcp -m conntrack --ctorigdstport 8080 -j DROP
Worked check — where does the connection fail?
docker port web # 80/tcp -> 0.0.0.0:8080
ss -ltnp | grep 8080 # docker-proxy or nothing (pure iptables)
curl -sI http://127.0.0.1:8080 # host → works?
docker exec web ss -ltn # app listening on 0.0.0.0:80, not 127.0.0.1:80?
If the app inside listens only on 127.0.0.1, traffic arriving on the container's eth0 is refused even though the port is published.
Common mistakes
- Publishing ports on 0.0.0.0 when only localhost is needed.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.