Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 12/ 160
01. Containerization & Docker Fundamentals

Port Mapping & Exposing Services (-p, -P)

Publish container ports to the host so browsers and clients can reach services.

Beginner-p · -P3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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/tcp for 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

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

Concrete -p setup

docker run -d --name admin -p 127.0.0.1:8080:80 nginx:1.27; docker port admin 80/tcp

Key takeaway: Prove -P with 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.

Deliberate failure test

Exercise this failure safely: publishing a database on every host interface while intending it for only one frontend container.

Key takeaway: Explicit host ports are easy to understand, but limit scale and scheduling; service discovery and an ingress usually replace them in multi-host systems.

How it works, step by step

Step 1 of 4
Design

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

A container listens on its private port, and -p explicitly gives it a host address and port.
Key numbers at this step
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}} admin in 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

Explicit -p
Best for: Publish container ports to the host so browsers and clients can reach services, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived -P experiments with low blast radius.

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.

Managed EXPOSE
Best for: Teams that centralize EXPOSE policy across many workloads.

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/tcp as proof and skipping ss -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/tcp address -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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Viewing Container Logs & Debugging (docker logs, inspect, stats)
Next topic
Managing Environment Variables (-e, --env-file)