Thuta Learning
IntermediateDevOps & Toolsintermediate

Port Mapping

Relax. We'll talk through this in plain words — no textbook voice.

Port Mapping means connecting a service port inside the container to a port on the host machine. The web server inside the container runs on port 80, but to reach it from your host browser, you need to publish a host port.

dockerfile
# Syntax
# docker run -p HOST_PORT:CONTAINER_PORT image_name

# Example: open Nginx through localhost:8080
docker run --name nginx-port-demo -d -p 8080:80 nginx

8080:80 — the left side, 8080, is the port on your computer, and the right side, 80, is the Nginx port inside the container.

You should see
You'll see the Nginx page at http://localhost:8080.

Info

A host port can't be used by more than one container at the same time. If you see a 'port already allocated' error, change the port or stop the old container.

Easy traps

  • If you flip the host/container port order, like writing -p 80:8080, the app won't show up. Remember the syntax is host:container.
Port Mapping | Thuta Learning