DEV Community

Darshan Vasani
Darshan Vasani Subscriber

Posted on • Edited on

🧭 Docker Port Mapping & CLI Flag CheatSheet

🧭 Docker Port Mapping & CLI Flag CheatSheet


πŸšͺ Port Mapping 101: -p vs -P

Flag Meaning Analogy Example
-p <host>:<container> Map host port to container port Custom Door Mapping πŸšͺ -p 8080:3000
-P (uppercase) Auto-map all exposed ports to random host ports πŸ”€ Auto Door Mapping -P

🎯 Example 1: Manual Mapping with -p

docker run -p 8080:3000 my-app 
Enter fullscreen mode Exit fullscreen mode
  • πŸ”§ Host port 8080 mapped to container's 3000
  • Access via localhost:8080

🎲 Example 2: Auto Mapping with -P

docker run -P my-app 
Enter fullscreen mode Exit fullscreen mode
  • Maps ALL EXPOSEd ports to random available host ports
  • View with docker ps

🌍 Multiple Port Mappings

docker run -p 3000:3000 -p 5000:5000 my-multi-app 
Enter fullscreen mode Exit fullscreen mode
  • Maps multiple services/APIs or frontend/backend apps
  • Great for full-stack containers!

πŸ“‘ Exposing Ports in Dockerfile

EXPOSE 3000 5000 7000 
Enter fullscreen mode Exit fullscreen mode
🧠 Note:
EXPOSE is just documentation for which ports the container listens to.
It does not publish or map ports. Use -p or -P in docker run to do that.

πŸ“¦ Exposing a Range of Ports

docker run -p 8000-8010:8000-8010 my-app 
Enter fullscreen mode Exit fullscreen mode
  • Useful for services like WebRTC, game servers, or load balancers needing multiple ports.
  • πŸ”„ Keep range symmetrical between host and container.

πŸ§ͺ Combine with --rm, -it, -d

Flag Meaning Emoji Use-case
--rm Auto-remove container when it exits 🧹 Clean test runs
-i Interactive (stdin open) 🎀 Needed for terminal apps
-t TTY (format output) πŸ–₯️ Pretty output formatting
-it Combo: interactive + tty πŸ§‘β€πŸ’» Needed for shell, REPLs
-d Detached mode (run in background) πŸ›Έ Long-running servers

πŸ”₯ Full Command Example

docker run -it --rm -p 4000:3000 my-app 
Enter fullscreen mode Exit fullscreen mode
  • Runs interactively
  • Auto deletes after exit
  • Maps port 3000 β†’ 4000

πŸš€ Run in Detached Mode + Multiple Ports + Clean Exit

docker run -d --rm -p 3000:3000 -p 5000:5000 my-app 
Enter fullscreen mode Exit fullscreen mode
  • Runs in background
  • Auto-cleans after stopping
  • Maps frontend + backend

πŸ•΅οΈβ€β™‚οΈ How to Check Mapped Ports?

docker ps 
Enter fullscreen mode Exit fullscreen mode
Column What it Shows
PORTS Host:Container mapping (e.g., 0.0.0.0:8080->3000/tcp)

πŸ›‘ Stop a Detached Container

docker stop <container_id> 
Enter fullscreen mode Exit fullscreen mode

🧠 Real-World Analogies

Concept Analogy
Container port πŸ“¦ Internal phone extension
Host port ☎️ Public phone number
-p Assigning specific number to extension
-P Letting system pick a number for you randomly

βœ… Summary Cheatsheet Table

Option Purpose Usage
-p 8080:80 Manual port map host:container
-p 8000-8010:8000-8010 Port range mapping Bulk apps
-P Auto-map all exposed ports Quick tests
EXPOSE 3000 Doc port in Dockerfile Doesn't publish
--rm Auto-remove after exit Clean containers
-it Interactive terminal Needed for shell
-d Run in background Daemonized apps

🧾 Real Full Example: Production-style Run

docker run -d --rm \ -p 8080:80 \ -p 443:443 \ --name my-nginx \ nginx:alpine 
Enter fullscreen mode Exit fullscreen mode

βœ… Starts Nginx with HTTP & HTTPS
βœ… Cleans itself after docker stop
βœ… Runs in background with custom name


🧠 Bonus Tip: Use --network to map ports between containers!

Top comments (0)