Docker Port Mapping Explained: -p, EXPOSE and Compose ports

Key Takeaways
- ✓The format is -p HOST_PORT:CONTAINER_PORT, host first.
- ✓EXPOSE only documents a port; -p publishes it.
- ✓Compose services reach each other by service name without published ports.
- ✓Published ports bypass UFW, so bind to 127.0.0.1 behind a reverse proxy.
Table of Contents
Docker Port Mapping Explained: -p, EXPOSE and Compose ports
A container has its own network, so a web server listening on port 80 inside it isn’t reachable from your browser until you publish that port to the host. That’s what port mapping does: -p 8080:80 sends traffic arriving on the host’s port 8080 to port 80 in the container. This guide explains the syntax, the difference between -p and EXPOSE, how ports work in Docker Compose, and the security detail most tutorials skip.
Quick answer: the format is -p HOST_PORT:CONTAINER_PORT. Use -p 127.0.0.1:8080:80 to publish only on localhost when a reverse proxy sits in front, because a plain -p 8080:80 is open to the internet and can bypass UFW.
The -p Syntax
| Option | Meaning |
|---|---|
-p 8080:80 | Host port 8080 on all interfaces → container port 80 |
-p 127.0.0.1:8080:80 | Only on the host’s localhost → container port 80 |
-p 80:80 -p 443:443 | Several ports |
-p 5353:53/udp | A UDP port |
-p 8000-8005:8000-8005 | A range |
-p 80 | Container port 80 → a random free host port |
-P | Publish every EXPOSEd port to random host ports |
docker run -d --name web -p 8080:80 nginx
curl http://localhost:8080The order matters: host first, container second. Getting it backwards is the most common mistake.
See Which Ports Are Published
docker ps # PORTS column, e.g. 0.0.0.0:8080->80/tcp
docker port web # mappings for one container0.0.0.0 (or [::]) means the port is open on every network interface, including your public IP.
EXPOSE Is Documentation, Not Publishing
EXPOSE 80 in a Dockerfile records which port the app listens on. It does not make the port reachable from the host. You still need -p (or -P, which publishes exposed ports to random host ports). Containers on the same Docker network can reach each other’s ports whether or not they’re exposed or published.
Ports in Docker Compose
services:
web:
image: nginx
ports:
- "127.0.0.1:8080:80"
api:
build: ./api
expose:
- "3000"
db:
image: postgres:17ports:publishes to the host, same syntax as-p. Quote the values, because YAML can misread some numbers with colons.expose:documents the port for other services; it isn’t published to the host.Services reach each other by service name on the Compose network: the web service connects to
http://api:3000and the API todb:5432. The database needs no published port at all.
The Security Detail: Published Ports Bypass UFW
Docker publishes ports by adding its own iptables rules, which are evaluated before UFW’s. So -p 5432:5432 on a server can expose your database to the internet even though ufw status shows the port as closed. Safe habits:
Don’t publish database or internal service ports at all; let containers talk over the Docker network.
Bind web apps to localhost (
127.0.0.1:8080:80) and put Nginx, Caddy or Traefik in front on 80/443.Use your cloud provider’s firewall as an outer layer.
More on this in how to open ports on a VPS.
Common Problems
“port is already allocated” / “address already in use”: another container or service uses the host port. Find it with
sudo ss -tlnp | grep :8080ordocker ps, then pick another host port.Connection refused from the host: the app inside listens on
127.0.0.1. Inside a container it must listen on0.0.0.0.Works on localhost, not from outside: it’s bound to
127.0.0.1, or a cloud firewall blocks it.Can’t change ports on a running container: port mappings are fixed when a container is created. Recreate it with the new
-p, or edit Compose and rundocker compose up -d.
Frequently Asked Questions
What does -p 8080:80 mean in Docker?
Traffic to port 8080 on the host goes to port 80 inside the container. The host port comes first.
What is the difference between EXPOSE and -p?
EXPOSE documents which port the app uses; -p actually publishes a port to the host so it can be reached from outside.
How do I publish a port only to localhost?
Add the IP: -p 127.0.0.1:8080:80. The port is then reachable only from the host itself, for example by a reverse proxy.
Do containers need published ports to talk to each other?
No. Containers on the same Docker network reach each other directly by name and internal port.
Can I add a port mapping to an existing container?
No. Recreate the container with the new mapping; with Compose, change ports and run docker compose up -d.
Run Containers Without Port Juggling
On RackUp Cloud Apps your container gets HTTPS on your domain automatically, with Git deploys and a private database, from $2.44 a month. Running your own Docker host? The Developer VPS gives you root access, 8 vCPU and 12 GB RAM for $35 a month. See plans.
Unsure Which Cloud Hosting Plan Fits Your Site?
Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.
Cloud Infrastructure Engineer.
Related Posts
DevOps & TutorialsDocker Engine vs Docker Desktop: Which One Do You Need?
Docker Engine is the open-source runtime for Linux; Docker Desktop is a desktop app with a VM, GUI and its own licence. Which to use on servers and laptops.
DevOps & TutorialsERR_CONNECTION_REFUSED: What It Means and How to Fix It
ERR_CONNECTION_REFUSED means your browser reached the address and the connection was rejected. Fixes for visitors, for localhost and for server owners.
DevOps & Tutorialsdocker compose up --build Explained (With Examples)
docker compose up --build builds images before starting containers. When you need it, when you don't, and the related flags people confuse, with examples.