0
0
Dockerdevops~10 mins

Port mapping with -p flag in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Port mapping with -p flag
Start Docker Container
Use -p flag: hostPort:containerPort
Docker maps hostPort to containerPort
Host can access container service via hostPort
Container listens on containerPort internally
End
This flow shows how Docker uses the -p flag to connect a port on your computer (host) to a port inside the container, allowing access to container services.
Execution Sample
Docker
docker run -p 8080:80 nginx
Runs an nginx container mapping host port 8080 to container port 80.
Process Table
StepCommand PartActionResultNotes
1docker runStart containerContainer created and startedBase command to run container
2-p 8080:80Map portsHost port 8080 linked to container port 80Enables access from host to container service
3nginxSelect imagenginx image pulled (if needed) and usedContainer runs nginx server
4Container runningListen on port 80nginx listens inside container on port 80Internal container port
5Host accessConnect to localhost:8080Request forwarded to container port 80User accesses nginx via host port
6ExitStop containerContainer stopsEnd of container lifecycle
💡 Container stops or user interrupts; port mapping ends with container lifecycle
Status Tracker
VariableStartAfter Step 2After Step 4Final
hostPortnone808080808080
containerPortnone808080
containerStatenot runningrunningrunningstopped
Key Moments - 3 Insights
Why do we write -p 8080:80 and not just -p 80?
The execution_table row 2 shows that -p 8080:80 maps host port 8080 to container port 80. Without specifying host port, Docker assigns a random port on the host, which may be hard to access.
Does the container listen on port 8080 inside?
No, as shown in execution_table row 4, the container listens on port 80 internally. Port 8080 is only on the host side.
What happens if the host port 8080 is already in use?
Docker will fail to start the container with an error because it cannot bind host port 8080. This is implied by the port mapping step in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what port does the container listen on internally?
A8080
B443
C80
DNone
💡 Hint
Check Step 4 in execution_table where container listens on port 80
At which step is the host port 8080 linked to the container port?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at Step 2 in execution_table where -p 8080:80 is processed
If you change the command to -p 9090:80, what changes in variable_tracker?
AhostPort changes to 9090
BcontainerPort changes to 9090
CcontainerState changes to 9090
DNo changes
💡 Hint
See variable_tracker hostPort value after Step 2
Concept Snapshot
docker run -p hostPort:containerPort image
- Maps a port on your computer (hostPort) to a port inside the container (containerPort).
- Allows accessing container services via localhost:hostPort.
- Container listens internally on containerPort.
- Host port must be free to bind.
- Example: docker run -p 8080:80 nginx
Full Transcript
This visual execution shows how Docker's -p flag maps a port from your computer to a container. When you run 'docker run -p 8080:80 nginx', Docker starts an nginx container listening on port 80 inside. The host port 8080 is linked to container port 80, so accessing localhost:8080 reaches nginx. The execution table traces each step: starting container, mapping ports, running nginx, and stopping container. Variables track hostPort and containerPort values. Key moments clarify why both ports are needed and what happens if ports conflict. The quiz tests understanding of port mapping steps and variable changes. This helps beginners see how Docker connects host and container ports clearly.