# Container Port Mapping

> A link from a host port to a port inside a box. It lets outside traffic in.

**Field:** Infrastructure  
**Category:** Containers  
**Also known as:** Port Publishing, Docker Port Mapping, Publish Port, docker run -p

## Think of it like

Think of it like a phone switchboard. A call to one number is passed to a desk inside.

## Definition

Port mapping connects a port on the host computer to a port inside a container. A container's ports are closed to the outside until you publish them. The flag -p 8080:80 sends traffic for host port 8080 to port 80 in the container. The host port comes first and the container port second. Two containers can both use port 80 inside, as long as each maps to a different host port.

## Example

Jack runs two web apps, each listening on port 80 inside its container. He maps one to host port 8080 and the other to 8081.

## Example code

```console
$ docker run -d -p 8080:80 nginx
$ curl -I localhost:8080
HTTP/1.1 200 OK
```

Host port 8080 now leads to port 80 inside the container, and the web server answers.

## Why it matters

Without it, nothing outside the container can reach the app. It is the first thing to check when a page will not load.

## Common mistake

Writing the ports the wrong way round. In -p 8080:80, the host port is first and the container port is second.

## Three principles

- **Closed by default.** Container ports are shut to the outside.
- **Host first.** The order is host port, then container port.
- **Many apps, one port.** Each maps to its own host port.

## Related terms

- [Container](https://learn.benibix.com/term/infrastructure/container)
- [Docker](https://learn.benibix.com/term/infrastructure/docker)
- [Network Port](https://learn.benibix.com/term/infrastructure/port)
- [Container Network](https://learn.benibix.com/term/infrastructure/container-network)
- [docker run](https://learn.benibix.com/term/infrastructure/docker-run)
- [Firewall](https://learn.benibix.com/term/infrastructure/firewall)

![A host box holding two containers, web and db, on a shared network line labelled shop. Host port 8080 has an arrow to port 80 in the web container. A host folder called project has a line into the web container's /app folder.](https://learn.benibix.com/diagrams/inf4-container-wiring.svg)

---

Source: https://learn.benibix.com/term/infrastructure/container-port-mapping
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
