0
0
Dockerdevops~10 mins

Port mapping in Compose in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Port mapping in Compose
Start Compose file
Define service
Specify ports mapping
Run docker-compose up
Docker maps host port to container port
Service accessible via host port
End
This flow shows how Docker Compose reads the ports section, maps host ports to container ports, and makes the service accessible.
Execution Sample
Docker
services:
  web:
    image: nginx
    ports:
      - "8080:80"
This Compose snippet maps host port 8080 to container port 80 for the web service.
Process Table
StepActionHost PortContainer PortResult
1Read Compose file--Service 'web' found with ports mapping
2Parse ports mapping808080Host port 8080 mapped to container port 80
3Run docker-compose up--Container starts with port mapping
4Access service808080Requests to localhost:8080 forwarded to container:80
5Exit--Port mapping active while container runs
💡 Port mapping ends when container stops or Compose is shut down
Status Tracker
VariableStartAfter Step 2After Step 3Final
host_port-808080808080
container_port-808080
service_statusnot runningnot runningrunningrunning
Key Moments - 2 Insights
Why do we write "8080:80" in ports instead of just "80"?
Because "8080:80" tells Docker to map port 8080 on your computer (host) to port 80 inside the container. Just "80" would map container port 80 to a random available host port.
What happens if the host port 8080 is already in use?
Docker will fail to start the container because it cannot bind to the busy port.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the host port mapped to container port 80?
A443
B80
C8080
D3000
💡 Hint
Check Step 2 in the execution table where ports mapping is parsed.
At which step does the container start running with port mapping?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look for the step mentioning 'Container starts with port mapping'.
If you change the ports line to "9090:80", what changes in the variable_tracker?
Ahost_port changes to 9090
Bcontainer_port changes to 9090
Cservice_status changes to stopped
DNo changes
💡 Hint
Host port is the first number in the mapping, check host_port row in variable_tracker.
Concept Snapshot
Port mapping in Docker Compose:
- Use 'ports:' under service to map host:container ports
- Syntax: 'host_port:container_port'
- Allows accessing container service via host port
- Host port must be free to bind
- Mapping active while container runs
Full Transcript
This visual execution shows how Docker Compose reads the ports mapping in a service definition. The Compose file defines a service named 'web' using the nginx image. The ports section maps host port 8080 to container port 80. When 'docker-compose up' runs, Docker starts the container and sets up the port forwarding. Requests to localhost:8080 on the host machine are forwarded to port 80 inside the container, making the web service accessible. The variable tracker shows host_port as 8080 and container_port as 80, with the service status changing from not running to running. Key moments clarify why the mapping syntax uses 'host:container' and what happens if the host port is busy. The quiz tests understanding of port mapping steps and variable changes.