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)