Beginner10 min readUpdated Jan 2025

Docker Ports Explained Simply — Visual Guide for Beginners

A beginner-friendly visual explanation of Docker ports. Learn how port mapping works, why containers have internal ports, how to expose ports using Dockerfile and docker run, and how host-to-container communication works.


1. Before Anything: Why Do We Need Ports in Docker?

Every container runs inside its own isolated world. Think of it like a separate mini-computer inside your computer.

So if your NGINX server runs inside Docker on port 80, only the container knows about it.

Your laptop has no idea that NGINX is running inside that container.

This is why we need port mapping.

It works like this:

Your Laptop Port  →  Container Port

Visual:

8080 (Host)
   ↓
   ↓  forwarded to
   ↓
80 (Container)

2. Visual Understanding of Docker Port Mapping

Here is the simplest representation:

docker run -p 8080:80 nginx

Visual:

 Host:8080  ─────→  Container:80

Meaning:

  • Container runs NGINX on port 80
  • You access it using localhost:8080

Port mapping is literally forwarding host traffic → inside the container.


3. How Ports Work Inside a Container

Every container has its own:

Network stack  
IP address  
Opened ports  

So two containers can both run on port 80 without conflict, because each lives in its own private network.

Example:

container1 → 80
container2 → 80
container3 → 80

No problem at all.

The issue comes when you want your host machine to access their ports.

So we map host ports → container ports.


4. Real Example — Running NGINX With Port Mapping

Run:

docker run -p 8080:80 nginx

Breakdown:

  • 8080 → Port on your laptop
  • 80 → Port inside container

Open:

http://localhost:8080

You now access the NGINX inside Docker.


5. General Formula of Port Mapping

docker run -p HOST_PORT:CONTAINER_PORT image

Examples:

-p 5000:5000  
-p 3000:3000  
-p 9000:80

You can map any host port to any container port.


6. EXPOSE in Dockerfile (What It Really Means)

Many beginners misunderstand EXPOSE.

Example:

EXPOSE 80

Important: EXPOSE does NOT open or publish the port. It only documents that the container intends to use port 80.

You still need docker run -p to expose it to outside world.


7. Visual Breakdown of EXPOSE vs -p

EXPOSE 80
↓
Dockerfile note (internal info only)

docker run -p 8080:80
↓
Actual port mapping done

EXPOSE = documentation -p = real port forwarding


8. Multiple Port Mapping Example

Say your app uses two ports: 80 and 443.

Run:

docker run -p 8080:80 -p 8443:443 nginx

Visual:

localhost:8080 → container:80  
localhost:8443 → container:443

9. Running Two Containers on Same Host Port?

Not possible.

Example:

docker run -p 8080:80 nginx
docker run -p 8080:80 apache

Second command fails because host port 8080 is already in use.

Solution:

Assign different host ports:

docker run -p 8080:80 nginx
docker run -p 8081:80 apache

10. Checking Ports Used by a Container

Run:

docker ps

You’ll see:

0.0.0.0:8080->80/tcp

Meaning:

  • 8080 → host
  • 80 → container

11. Docker Networking + Ports (Clear Visual)

Inside the same Docker network:

containers talk using names (no ports exposed)

Outside world:

requires port mapping

So port mapping is only required for external access, not container-to-container.


12. 5-Second Visual Summary

Container opens port → host cannot see it  
docker run -p maps host → container  
EXPOSE is only documentation  
Containers can share port numbers  
Host ports must be unique

Done.


13. FAQs (SEO-Friendly + Beginner Questions)

Q1: Does EXPOSE open a port to the outside?

No. It is only documentation.


Q2: Can two containers use port 80 internally?

Yes. They have isolated network stacks.


Q3: Can two containers use host port 8080?

No. Host ports must be unique.


Q4: Can Docker automatically choose a port?

Yes:

docker run -p 80

Docker will pick a random free host port.


Q5: Do I need port mapping for container-to-container communication?

No. Containers talk internally using Docker networks.


🏁 Final Words (Your Tone)

Docker ports are not complicated at all. They are just “doorways” for your container so the outside world can reach it.

Inside the container → port is private Outside the container → host must forward traffic

Once you understand this visually, Docker networking starts making complete sense.


Mastered this concept?

Take the next step in your journey to becoming a senior developer.