Docker Containers
Estimated reading: 4 minutes 105 views

🔌 Docker Container Port Mapping – Expose Your Apps to the World

🧲 Introduction – Why Learn Docker Port Mapping?

When you run services like web servers, databases, or APIs inside a Docker container, those services are isolated within the container’s private network. By default, they are inaccessible from your host machine or the internet.

That’s where Docker port mapping comes in. It bridges the gap between the container and the outside world, letting you connect to applications running inside containers as if they were running natively on your machine.

🎯 In this guide, you’ll learn:

  • What port mapping is and why it’s important
  • How to expose ports using docker run -p
  • How to check which ports are mapped
  • Tips and commands for advanced port inspection

🧩 What is Docker Port Mapping?

Docker port mapping links a port on your host machine to a port inside the container. This allows external tools (like your browser or Postman) to access services running within containers.

📌 Example Use Case:

  • A container runs a web server on port 80
  • You map it to your host machine’s port 8080

👉 Visiting http://localhost:8080 on your host gives you access to the container’s web server.


🔧 How to Do Port Mapping in Docker

The most common way to map ports is with the -p flag during docker run.

Syntax:

docker run -td --name <container_name> -p <host_port>:<container_port> <image>

Example:

docker run -td --name container1 -p 8080:80 ubuntu

🔹 This maps host port 8080 to container port 80

🔹 You can now access the container’s web app at: http://localhost:8080


🔄 Mapping Multiple Ports

Docker also supports mapping multiple ports in one command.

Example:

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

This maps:

  • Host port 8080 → Container port 80 (HTTP)
  • Host port 443 → Container port 443 (HTTPS)

🔍 How to Check Ports Used by Docker Containers

Once you’ve set up port mappings, there are several ways to view them.


📋 1. Using docker ps

docker ps

Sample Output:

CONTAINER ID   IMAGE          PORTS                     
a1b2c3d4e5f6   nginx:latest   0.0.0.0:8080->80/tcp

🔹 This tells you which host port is mapped to which container port


🔌 2. Using docker port

docker port <container_id_or_name>

✅ Example:

docker port webserver

🔎 Output:

80/tcp -> 0.0.0.0:8080

🔬 3. Using docker inspect

docker inspect <container_id_or_name>

To focus only on port mappings:

docker inspect --format='{{json .NetworkSettings.Ports}}' <container_id>

Or with jq:

docker inspect <container_id> | jq '.[0].NetworkSettings.Ports'

📘 Summary Table – Docker Port Mapping Commands

🔧 Task🧪 Command Example
Run container with port mappingdocker run -p 8080:80 nginx
Map multiple portsdocker run -p 8080:80 -p 443:443 nginx
Check all running container portsdocker ps
Check ports of a specific containerdocker port webserver
Inspect detailed port configurationdocker inspect <container_id>
View exposed (but unpublished) portsCheck .Config.ExposedPorts in inspect output

🧾 Final Thoughts

Port mapping is essential when running Docker containers that need to interact with external systems, users, or other apps.

Mastering docker run -p, docker ps, and docker port gives you complete control over which services are accessible and how they’re exposed.

🚀 Unlock your containerized apps with smart port mapping – simple, powerful, and production-ready!


❓ Frequently Asked Questions (FAQs)


Q1: How can I list ports for all running containers?

docker ps

✅ Displays port mappings under the “PORTS” column.


Q2: Can I check ports of a stopped container?

Yes!

docker inspect <container_id>

Q3: How do I find exposed but unpublished ports?

Inspect the container:

docker inspect <container_id>

➡️ Look under:

.Config.ExposedPorts

Q4: How to see both TCP and UDP ports?

Check detailed mappings in the inspect output:

"80/tcp": [{ "HostIp": "0.0.0.0", "HostPort": "8080" }],
"53/udp": null

Q5: Can I bind the same container port to different host ports?

Yes! Example:

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

✅ This binds container port 80 to two different host ports.


Share Now :
Share

Docker Container Port Mapping

Or Copy Link

CONTENTS
Scroll to Top