Port mapping in Compose in Docker - Time & Space Complexity
We want to understand how the time it takes to set up port mapping in Docker Compose changes as we add more services or ports.
How does the setup time grow when the number of ports or services increases?
Analyze the time complexity of the following Docker Compose port mapping configuration.
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
- "8081:81"
api:
image: myapi
ports:
- "9090:90"
This snippet maps multiple host ports to container ports for two services.
Look for repeated steps in setting up port mappings.
- Primary operation: Mapping each port from host to container.
- How many times: Once for each port listed in the Compose file.
Each additional port adds one more mapping step.
| Input Size (n ports) | Approx. Operations |
|---|---|
| 10 | 10 mapping steps |
| 100 | 100 mapping steps |
| 1000 | 1000 mapping steps |
Pattern observation: The setup time grows directly with the number of ports.
Time Complexity: O(n)
This means the time to set up port mapping grows linearly with the number of ports you define.
[X] Wrong: "Adding more ports won't affect setup time because Docker handles ports instantly."
[OK] Correct: Each port requires a separate mapping step, so more ports mean more work during setup.
Understanding how configuration size affects setup time helps you explain system behavior clearly and shows you think about scaling practically.
What if we changed from mapping ports individually to using a range of ports? How would the time complexity change?
Practice
ports section in a Docker Compose file do?Solution
Step 1: Understand the role of
Theportsin Composeportssection connects a port on your computer (host) to a port inside the container.Step 2: Identify what
It does not set environment variables, resource limits, or restart policies.portsdoes not doFinal Answer:
It maps ports from the host machine to the container ports. -> Option DQuick Check:
Port mapping = connecting host to container ports [OK]
- Confusing ports with environment variables
- Thinking ports set resource limits
- Mixing ports with restart policies
Solution
Step 1: Understand port mapping format
The format ishost_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.Final Answer:
ports: - "8080:80" -> Option AQuick Check:
Host port first, container port second [OK]
- Reversing host and container ports
- Using single port without mapping
- Incorrect YAML indentation
services:
web:
image: nginx
ports:
- "5000:80"
What happens when you open http://localhost:5000 in your browser?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
Openinghttp://localhost:5000sends 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 BQuick Check:
Host port 5000 maps to container port 80 [OK]
- Thinking port 80 is not exposed
- Confusing host and container ports
- Expecting file content instead of service
ports: - "80:8080"But your app inside the container listens on port 8080. Why can't you access it via
http://localhost:80?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 CQuick Check:
Check app listens on container port [OK]
- Assuming ports reversed when they are correct
- Ignoring app's actual listening port
- Blaming syntax without checking app
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.Final Answer:
web: ports: - "3000:3000" api: ports: - "5000:5000" -> Option AQuick Check:
Host and container ports must match for access [OK]
- Reversing host and container ports
- Using single port without mapping
- Mapping both services to same container port
Ready to prove it?
Pass the test and this pattern moves unknown
