-p 8080:80 is the line everyone copies and few fully understand. Which number is the host? What happens if you swap them? Why does 443:443 look different from 8080:80 when it’s the same mechanism? And why does a container that’s clearly running still refuse connections?
Port mapping is just forwarding: a port on the host hands traffic to a port inside the container. Once the direction is clear, the rest — localhost binding, UDP, ranges, the difference between EXPOSE and publishing — falls into place. Getting it wrong, on the other hand, either makes your app unreachable or quietly exposes a database to the whole internet.
This guide nails down the syntax, the security-critical 127.0.0.1 trick, and the handful of mistakes that account for most “it’s running but I can’t reach it” problems.
The mapping is host:container
The publish flag is -p, and the format is host port first, container port second:
docker run -d -p 8080:80 nginx
That reads: take traffic arriving on the host’s port 8080 and forward it to port 80 inside the container. Nginx inside the container listens on 80, as it always does; you reach it from your browser at http://localhost:8080. The container never knows or cares that the outside world used 8080.
The two numbers are independent. You can map any free host port to whatever port the app uses internally:
Reading port mappings
| -p 8080:80 | Host 8080 → container 80. Reach it at localhost:8080. |
|---|---|
| -p 80:80 | Host 80 → container 80. Standard HTTP, same number both sides. |
| -p 443:443 | Host 443 → container 443. Standard HTTPS, same number both sides. |
| -p 3000:3000 | Host 3000 → container 3000. Common for Node dev servers. |
| -p 5433:5432 | Host 5433 → container 5432. Run Postgres on a non-default host port. |
So 443:443 isn’t special — it’s the same host:container rule with matching numbers, because HTTPS conventionally uses 443 on both sides. There’s no hidden meaning; people just map the standard port to itself.
Why a running container can still refuse connections
This is the most common support question, and it almost always comes down to one of three things.
You didn’t publish the port. A container with no -p is reachable by other containers on its network, but not from your host. Add the mapping.
You published the wrong container port. The right-hand number must match what the app actually listens on inside the container. If a Node app listens on 3000 but you wrote -p 8080:80, you’re forwarding to port 80 where nothing’s running. Check the app’s real port and the image’s EXPOSE line.
The app binds to 127.0.0.1 inside the container. This one’s sneaky. If the process inside the container listens on 127.0.0.1 (localhost) instead of 0.0.0.0 (all interfaces), it only accepts connections from inside the container. Docker’s forwarded traffic arrives on the container’s external interface, gets refused, and you see “connection refused” despite a perfect -p flag.
# Inside the container, the app must listen on 0.0.0.0, not 127.0.0.1
# e.g. a dev server flag like --host 0.0.0.0, or app config binding to all interfaces
Binding to 127.0.0.1: the security-critical part
By default, -p 5432:5432 binds the host port to all host interfaces — 0.0.0.0. On a laptop behind a router that’s mostly fine. On a VPS with a public IP, it means your database is now reachable from the internet. This is one of the most common ways people accidentally expose a database.
Prefix the host port with 127.0.0.1 to bind it to the loopback interface only:
# Reachable only from the host itself — not the LAN, not the internet
docker run -d -p 127.0.0.1:5432:5432 postgres:16
Now the port answers only to connections originating on the host (local tooling, an SSH tunnel), never from outside. Use this for anything that doesn’t need to be public: databases, admin panels, metrics endpoints, message brokers.
Where a published port is reachable from
| -p 5432:5432 | All interfaces (0.0.0.0). LAN and public IP included — risky on a server. |
|---|---|
| -p 127.0.0.1:5432:5432 | Host only. Safe default for databases and internal tools. |
| -p 10.0.0.5:5432:5432 | A specific host interface only, e.g. a private network NIC. |
| No -p at all | Not reachable from the host; only from containers on the same network. |
The best option for an internal service is often no published port at all. If only other containers talk to it, leave it on a shared Docker network and skip -p entirely — the Docker networking guide explains why containers reach each other by name without any publishing.
UDP, ranges, and ephemeral ports
TCP is the default. For UDP services, append /udp:
# DNS over UDP
docker run -d -p 53:53/udp mydnsimage
Some services need both protocols on the same port — publish it twice:
docker run -d -p 53:53/tcp -p 53:53/udp mydnsimage
You can publish a contiguous range in one go:
docker run -d -p 7000-7010:7000-7010 myapp
And if you don’t care which host port you get, give only the container port and let Docker pick a random high one:
docker run -d -p 80 nginx
docker port <container> # shows which host port Docker assigned
Ephemeral mapping is handy for throwaway tests and for running many copies of the same image without planning ports. For anything you’ll connect to deliberately, pin the host port.
EXPOSE is documentation, not publishing
A Dockerfile EXPOSE 80 line trips people up because it sounds like it opens a port. It doesn’t. EXPOSE is metadata — it documents which port the app uses, and it’s what -P (capital P) reads to auto-publish. By itself it makes nothing reachable from the host.
EXPOSE 80 # documents the port; does NOT publish it
docker run -d -P myimage # -P publishes every EXPOSEd port to random host ports
So EXPOSE plus -P auto-publishes to random ports; EXPOSE alone does nothing for host access. To actually control where a port lands, use explicit -p host:container. If you hit a clash between two containers wanting the same host port, the port already allocated fix walks through finding and freeing it.
Port mapping in Docker Compose
Compose uses a ports: list, and each entry follows the same host:container rule. The long and short forms both work:
services:
web:
image: nginx
ports:
- "8080:80" # host 8080 → container 80
db:
image: postgres:16
ports:
- "127.0.0.1:5432:5432" # localhost-only, safe on a server
environment:
POSTGRES_PASSWORD: change-me
The same security point applies in Compose: that 127.0.0.1 prefix on the database is what keeps it off the network. Better still, if web is the only thing that talks to db, drop the ports: block from db completely and let them communicate over the project network. The long syntax spells out each field if you prefer it explicit:
ports:
- target: 80
published: 8080
protocol: tcp
mode: host
Quick reference
Port mapping sanity checks
- Order is host:container — host port first, always
- The container port must match what the app listens on inside
- The app inside must bind 0.0.0.0, not 127.0.0.1
- Bind public-server databases to 127.0.0.1 on the host
- Internal-only services need no -p at all
- Add /udp for UDP services; publish twice for TCP+UDP
- EXPOSE documents a port; -p (or -P) actually publishes it
Wrapping up
Port mapping is forwarding with a fixed grammar: -p host:container, host first. Match the container side to whatever the app listens on, make sure that app binds 0.0.0.0 inside, and you’ve solved most connectivity problems. 443:443 and 8080:80 are the same rule with different numbers — nothing more.
The part worth burning into memory is the security default: a bare -p 5432:5432 on a public server exposes that service to the internet, while -p 127.0.0.1:5432:5432 keeps it local, and no -p at all keeps it strictly between containers. Choose deliberately for every port you publish. For the bigger picture on how containers talk without publishing, read Docker networking for beginners; to resolve clashes, see fixing port already allocated; and browse more in the Docker & Containers guides.