0
0
Dockerdevops~10 mins

Exposing ports to host in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Exposing ports to host
Start Docker Container
Specify Port Mapping
Docker Maps Container Port to Host Port
Host Can Access Container Service via Host Port
End
This flow shows how Docker maps a container's internal port to a port on the host machine, allowing access from outside the container.
Execution Sample
Docker
docker run -p 8080:80 nginx
Runs an nginx container exposing container port 80 on host port 8080.
Process Table
StepActionContainer PortHost PortResult
1Start container with -p 8080:80808080Docker maps container port 80 to host port 8080
2Container runs nginx server on port 80808080nginx listens inside container on port 80
3Host accesses localhost:8080808080Request forwarded to container port 80, nginx responds
4Stop container--Port mapping removed, host cannot access container service
💡 Container stopped, port mapping no longer active
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Container Port-808080-
Host Port-808080808080-
Container RunningNoYesYesYesNo
Host AccessNoNoNoYesNo
Key Moments - 3 Insights
Why do we write -p 8080:80 instead of just -p 80?
Because -p 8080:80 tells Docker to map container port 80 to host port 8080. Without specifying host port, Docker assigns a random port, making it hard to access predictably. See execution_table step 1.
Does exposing a port mean the container service is accessible from outside the host machine?
Not necessarily. Exposing ports maps container ports to host ports, but external access depends on host firewall and network settings. The execution_table shows host access at localhost:8080, which is local to the host.
What happens if the container stops running?
Port mapping stops and host cannot access the container service anymore. See execution_table step 4 where container stops and port mapping is removed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the host start being able to access the container service?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Host Access' variable in variable_tracker after each step.
If we change the command to 'docker run -p 9090:80 nginx', what will be the host port in the execution table?
A80
B9090
C8080
DRandom port
💡 Hint
The host port is the first number in -p hostPort:containerPort mapping, see execution_table step 1.
According to the variable_tracker, what is the state of 'Container Running' after step 4?
ANo
BYes
CUnknown
DDepends on host
💡 Hint
Look at the 'Container Running' row in variable_tracker under 'Final' column.
Concept Snapshot
Docker port exposing syntax: docker run -p hostPort:containerPort image
Maps container's internal port to a port on the host.
Host accesses container service via hostPort.
Port mapping ends when container stops.
Useful for accessing web servers or APIs inside containers.
Full Transcript
This visual execution shows how Docker exposes container ports to the host machine. We start a container with the command 'docker run -p 8080:80 nginx' which maps container port 80 to host port 8080. The container runs nginx listening on port 80 internally. When the host accesses localhost:8080, the request is forwarded to the container's port 80 and nginx responds. When the container stops, the port mapping is removed and the host can no longer access the service. Variables like container port, host port, container running state, and host access change step by step. Key points include specifying both host and container ports in -p, understanding local host access, and that port mapping ends when the container stops.