DockerPlay
1
Chapter 0510 mins • beginner

Ports & Port Publishing

Host Port vs Container Port Mapping & Network Address Translation

What You Will Master:

Understand the syntax of `-p HOST_PORT:CONTAINER_PORT`
Learn why container ports are invisible to the host without `-p`
Avoid host port collision errors

Anatomy of `-p 8080:80`

By default, containers exist inside an isolated private subnet. Their internal ports (e.g. port 80 for Nginx or 5432 for Postgres) cannot be reached directly from your browser or host network. The `-p` flag instructs Docker to create a NAT port forward bridge: ```text Browser -> http://localhost:8080 (Host Port) │ (Docker NAT Forwarding) ▼ Container Port 80 (Nginx) ```
Interactive Lab

Publish Nginx on Port 8080

Run an nginx container exposed on host port 8080.

0/1 Steps Done
Step 1: Publish nginx on host port 8080

Run `docker run -d --name web-server -p 8080:80 nginx:alpine`

Chapter Knowledge Check

Take the interactive MCQ test to unlock bonus XP

Sponsor & Resources