DEV Community

Darshan Vasani
Darshan Vasani Subscriber

Posted on • Edited on

Port Mapping

Port Mapping


πŸšͺ Docker Port Mapping = Connecting Your App to the Outside World

πŸ’‘ Think of a Docker container like a house 🏠 and ports as doors πŸšͺ.
You need to open a door in the container AND connect it to your host so people can visit. πŸ§‘β€πŸ’»πŸŒ


🎯 Syntax

docker run -p <hostPort>:<containerPort> <image>
Enter fullscreen mode Exit fullscreen mode
Element Description Emoji Analogy
hostPort Port on your local machine (PC) πŸ§‘β€πŸ’» Door outside the house
containerPort Port inside the Docker container 🏠 Door inside the house

βœ… Common Examples

1. πŸ” Same port inside & outside

docker run -p 3000:3000 my-app
Enter fullscreen mode Exit fullscreen mode
  • App runs on port 3000 in container.
  • Accessible at http://localhost:3000 on host.

🧠 You "opened the same door" on both sides.


2. πŸ”€ Different ports (host vs container)

docker run -p 8080:3000 my-app
Enter fullscreen mode Exit fullscreen mode
  • App runs on port 3000 inside.
  • Accessible at http://localhost:8080 outside.

🧠 You redirected visitors from door 8080 to 3000 inside.


3. πŸ”’ Multiple ports

docker run -p 8000:8000 -p 5000:5000 my-app
Enter fullscreen mode Exit fullscreen mode

Multiple doors open πŸ”“ for frontend/backend or API/UI separation.


4. 🌐 Binding to specific IP (advanced)

docker run -p 127.0.0.1:8080:80 nginx
Enter fullscreen mode Exit fullscreen mode
  • Only accessible from localhost, not from other devices.

πŸ§ͺ Testing Port Mappings

Test Method Example Command Purpose
🌐 Web Browser http://localhost:8080 Access frontend/backend
πŸ§ͺ curl CLI Tool curl localhost:8080 API/health check
πŸ” Docker Inspect docker container inspect <id> See exposed ports inside
πŸ“¦ Docker PS docker ps Shows active port mappings

❗ Common Pitfalls & Fixes

Problem Cause Fix
❌ Cannot access app on browser You forgot -p Always map with -p!
πŸ”’ Firewall blocking access System-level rules Allow port in your firewall
🚫 Conflicting ports Same host port already in use Change host port (e.g., -p 8081:3000)

πŸš€ Bonus Tip: EXPOSE in Dockerfile

EXPOSE 3000
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ This is for documentation only! It does not publish the port β€” you still need -p during docker run.


🧠 Quick Reference Table

CLI Option Meaning Example
-p 8080:80 Host port 8080 β†’ Container port 80 localhost:8080 opens NGINX
-p 127.0.0.1:8000:8000 Bind to specific host IP Secure dev testing
EXPOSE in Dockerfile Internal doc (not binding) Use for image readability

🧭 Final Analogy: Port Mapping = Mail Forwarding πŸ“¬

Imagine:

  • 🏠 Container address = Internal port (e.g. 3000)
  • πŸ§‘β€πŸ’» Public address = Host port (e.g. 8080)
  • πŸ” Port mapping forwards public requests to the container, like mail forwarding!

βœ… Summary

Step What to Do
1️⃣ Decide which port your app runs on inside the container
2️⃣ Map it using -p hostPort:containerPort
3️⃣ Access it via localhost:hostPort
4️⃣ Check with docker ps to confirm mapping

Top comments (0)