Docker Containers
Estimated reading: 4 minutes 37 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Docker Container Port Mapping

Or Copy Link

CONTENTS
Scroll to Top