📖 Scenario: You are setting up a simple web application using Docker Compose. The application runs inside a container and listens on port 80. To access it from your computer's browser, you need to map the container's port 80 to a port on your computer.
🎯 Goal: Learn how to write a Docker Compose file that maps a container port to a host port so you can access the application from your browser.
📋 What You'll Learn
Create a Docker Compose file named docker-compose.yml
Define a service called webapp using the nginx image
Map container port 80 to host port 8080 using port mapping
💡 Why This Matters
🌍 Real World
Port mapping is essential to access applications running inside containers from your computer or other devices on the network.
💼 Career
Understanding Docker Compose port mapping helps in deploying and managing containerized applications in development and production environments.
Progress0 / 4 steps
1
Create the basic Docker Compose file
Create a file named docker-compose.yml and write the starting lines to define the version as '3.8' and an empty services section.
Docker
Hint
Start with version: '3.8' on the first line, then add services: below it.
2
Add the webapp service with nginx image
Under the services section, add a service named webapp that uses the nginx image.
Docker
Hint
Indent the webapp service under services: and specify image: nginx under it.
3
Add port mapping to expose container port 80 on host port 8080
Inside the webapp service, add a ports section that maps host port 8080 to container port 80.
Docker
Hint
Use ports: followed by - "8080:80" indented properly under webapp.
4
Run Docker Compose and check port mapping
Run docker compose up -d in your terminal to start the container. Then run docker compose ps and write the output showing the port mapping line for the webapp service.
Docker
Hint
After starting the container, docker compose ps shows the port mapping like 0.0.0.0:8080->80/tcp.
Practice
(1/5)
1. What does the ports section in a Docker Compose file do?
easy
A. It defines the container's CPU and memory limits.
B. It sets environment variables inside the container.
C. It specifies the container's restart policy.
D. It maps ports from the host machine to the container ports.
Solution
Step 1: Understand the role of ports in Compose
The ports section connects a port on your computer (host) to a port inside the container.
Step 2: Identify what ports does not do
It does not set environment variables, resource limits, or restart policies.
Final Answer:
It maps ports from the host machine to the container ports. -> Option D
Quick Check:
Port mapping = connecting host to container ports [OK]
Hint: Ports section maps host ports to container ports [OK]
Common Mistakes:
Confusing ports with environment variables
Thinking ports set resource limits
Mixing ports with restart policies
2. Which of the following is the correct syntax to map port 8080 on the host to port 80 in a Docker Compose service?
easy
A. ports:
- "8080:80"
B. ports:
- "80"
C. ports:
- "8080"
D. ports:
- "80:8080"
Solution
Step 1: Understand port mapping format
The format is host_port:container_port. To map host 8080 to container 80, write "8080:80".
Step 2: Check each option
ports:
- "80:8080" reverses ports, mapping host 80 to container 8080, which is incorrect. Options B and C specify only one port, which publishes the container port to a randomly assigned host port, not specific host 8080.
What happens when you open http://localhost:5000 in your browser?
medium
A. You get a connection refused error because port 80 is not exposed.
B. You access the Nginx server running inside the container on port 80.
C. You access port 5000 inside the container, which is not running any service.
D. The browser will show the Docker Compose file content.
Solution
Step 1: Understand port mapping in the snippet
Port 5000 on the host is mapped to port 80 inside the container where Nginx listens.
Step 2: Explain browser behavior
Opening http://localhost:5000 sends requests to container's port 80, so you reach the Nginx server.
Final Answer:
You access the Nginx server running inside the container on port 80. -> Option B
Quick Check:
Host port 5000 maps to container port 80 [OK]
Hint: Host port connects to container port service [OK]
Common Mistakes:
Thinking port 80 is not exposed
Confusing host and container ports
Expecting file content instead of service
4. You wrote this Compose ports mapping:
ports:
- "80:8080"
But your app inside the container listens on port 8080. Why can't you access it via http://localhost:80?
medium
A. The container port 8080 is not exposed correctly to the host port 80.
B. The ports are reversed; host port should be 8080 and container port 80.
C. The app inside the container is not running on port 8080.
D. The Compose file syntax is invalid.
Solution
Step 1: Analyze the port mapping
Mapping "80:8080" means host port 80 connects to container port 8080.
Step 2: Identify the problem
If access fails, likely the container app is not listening on port 8080 or firewall blocks host port 80. The mapping syntax is correct.
Final Answer:
The app inside the container is not running on port 8080. -> Option C
Quick Check:
Check app listens on container port [OK]
Hint: Check app listens on container port, not just mapping [OK]
Common Mistakes:
Assuming ports reversed when they are correct
Ignoring app's actual listening port
Blaming syntax without checking app
5. You want to run two services in Compose: a web app on container port 3000 and an API on container port 5000. You want to access them on host ports 3000 and 5000 respectively. Which Compose ports section is correct?
hard
A. web:
ports:
- "3000:3000"
api:
ports:
- "5000:5000"
B. web:
ports:
- "3000:80"
api:
ports:
- "5000:80"
C. web:
ports:
- "80:3000"
api:
ports:
- "80:5000"
D. web:
ports:
- "3000"
api:
ports:
- "5000"
Solution
Step 1: Match host and container ports
To access web on host 3000 and container 3000, map "3000:3000". Similarly for API, map "5000:5000".
Step 2: Check other options
web:
ports:
- "3000:80"
api:
ports:
- "5000:80" maps container ports incorrectly to 80. web:
ports:
- "80:3000"
api:
ports:
- "80:5000" reverses ports. web:
ports:
- "3000"
api:
ports:
- "5000" uses short syntax without mapping, which assigns random host ports.