Pcornel
Browse topics
Options
Recent
Favorites
Containers · Technical reference

Published Ports and Port Mapping

Direct answer

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.

When to use this

Use it when a containerised service is not reachable from outside.

First 30 seconds

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>.

Operational verification

client → host port → NAT → container port → app

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Docker / containerd / Podman / Kubernetes / OCI-compatible platforms
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformDocker / containerd / Podman / Kubernetes / OCI-compatible platforms
Scope / prerequisiteUse it when a containerised service is not reachable from outside.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

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.

Key checks
  • docker ps -a — containers and exit codes
  • docker logs <c> — container output
  • docker 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

FlagListens on
-p 8080:80All host addresses, IPv4 and IPv6
-p 127.0.0.1:8080:80Loopback only
-p 192.0.2.10:8080:80One host address
-p 8080:80/udpUDP instead of TCP
-PEvery 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
published ports and port mappingdocker publish portDOCKER-USER chain
Confirmation

Confirm action

Continue?