Port Mapping & Exposing Services (-p, -P)
Publish container ports to the host so browsers and clients can reach services.
In plain words
A container listens on its private port, and -p explicitly gives it a host address and port.
Think of it like…
EXPOSE labels which door the building plans to use; -p installs the numbered entrance on the host street.
Where you’ve seen it
In practice, docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp is the concrete move for this job: publish container ports to the host so browsers and clients can reach services. The release is not accepted until ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin confirms the result.
Words you’ll hear
- -p
- The implementation control in
docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcpfor publish container ports to the host so browsers and clients can reach services. - -P
- The result to observe with
ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin, rather than infer from a successful command alone. - EXPOSE
- The production boundary exposed when publishing a database on every host interface while intending it for only one frontend container.
Visual diagram
Theory & deep dive
-p control path
The concrete mechanism is docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp. The observable result is ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin; if those disagree, inspect the runtime evidence before changing unrelated settings.
Publish container ports to the host so browsers and clients can reach services. Keep -p, -P and EXPOSE in the same deployment record so the intent and the observed state cannot drift apart.
docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp
ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin, not with command success alone.EXPOSE failure boundary
EXPOSE or the right container listener is not enough to make a service reachable from a browser. A broad host binding can accidentally expose admin panels, bypass a reverse proxy and conflict with host firewalls. The narrow failure to design for is publishing a database on every host interface while intending it for only one frontend container.
Accept this compromise explicitly: Explicit host ports are easy to understand, but limit scale and scheduling; service discovery and an ingress usually replace them in multi-host systems. The controlled check-ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin-also identifies the point where a different design is safer.
Exercise this failure safely: publishing a database on every host interface while intending it for only one frontend container.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: EXPOSE or the right container listener is not enough to make a service reachable from a browser. A broad host binding can accidentally expose admin panels, bypass a reverse proxy and conflict with host firewalls.
- Intent
- -p
When to use it & trade-offs
What it is
Publishing -p maps a host socket to a container port through Docker networking; EXPOSE only records image metadata and does not publish. Binding 127.0.0.1:8080:80 keeps the service local, whereas 0.0.0.0:8080:80 accepts connections on host interfaces.
Why it matters: EXPOSE or the right container listener is not enough to make a service reachable from a browser. A broad host binding can accidentally expose admin panels, bypass a reverse proxy and conflict with host firewalls.
When to reach for it
- The fit is strongest for this job: publish container ports to the host so browsers and clients can reach services. Pair it with
ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} adminin the runbook. - Production -p changes should make -P observable through
docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp, rather than trusting the runtime default. - Keep a rollback example for the case where publishing a database on every host interface while intending it for only one frontend container.
When to avoid or be careful
- Do not accept command success while the system can still fail by publishing a database on every host interface while intending it for only one frontend container.
- Avoid erasing EXPOSE protection merely to dodge this cost: “Explicit host ports are easy to understand, but limit scale and scheduling; service discovery and an ingress usually replace them in multi-host systems”.
- Do not replace the evidence path-
ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin-with a screenshot or a mutable tag.
docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp: practical choices
Strengths: Publishing -p maps a host socket to a container port through Docker networking; EXPOSE only records image metadata and does not publish. Binding 127.0.0.1:8080:80 keeps the service local, whereas 0.0.0.0:8080:80 accepts connections on host interfaces.
Watch out: Explicit host ports are easy to understand, but limit scale and scheduling; service discovery and an ingress usually replace them in multi-host systems.
Strengths: It can begin with docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp and fewer surrounding controls.
Watch out: It silently accepts publishing a database on every host interface while intending it for only one frontend container, which makes the result hard to reproduce after promotion.
Strengths: EXPOSE or the right container listener is not enough to make a service reachable from a browser. A broad host binding can accidentally expose admin panels, bypass a reverse proxy and conflict with host firewalls.
Watch out: The platform adds an ownership and debugging layer; retain ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp
- ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin
- Failure signature: publishing a database on every host interface while intending it for only one frontend container
- Trade-off: Explicit host ports are easy to understand, but limit scale and scheduling; service discovery and an ingress usually replace them in multi-host systems.
Benchmarks & rules of thumb
- 1 mapping
- host IP, host port and container port form the tuple
- 0.0.0.0
- all IPv4 host interfaces, not a security allowlist
- 65535
- maximum TCP or UDP port number
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcpas proof and skippingss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin. - Publishing a database on every host interface while intending it for only one frontend container
- Optimizing -p without accepting that explicit host ports are easy to understand, but limit scale and scheduling; service discovery and an ingress usually replace them in multi-host systems.
Likely interviewer follow-ups
- Why does
docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcpaddress -p here rather than through application code? - How would you distinguish healthy -P from the failure “publishing a database on every host interface while intending it for only one frontend container” using
ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin? - When would this trade-off-“Explicit host ports are easy to understand, but limit scale and scheduling; service discovery and an ingress usually replace them in multi-host systems”-justify a different EXPOSE design?
docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp-and one proof-ss -lntp and curl -fsS http://127.0.0.1:8080 from the host, then docker inspect --format {{json .NetworkSettings.Ports}} admin. Then name the boundary: publishing a database on every host interface while intending it for only one frontend container.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Port Mapping & Exposing Services (-p, -P) with generative AI interview Q&A and hands-on practice.