Exposing ports to host in Docker - Time & Space Complexity
We want to understand how the time it takes to expose ports in Docker changes as we expose more ports.
How does adding more ports affect the work Docker does to set up networking?
Analyze the time complexity of the following Docker command snippet.
docker run -d \
-p 8080:80 \
-p 8081:81 \
-p 8082:82 \
nginx
This command runs a container and exposes three ports from the container to the host machine.
Look for repeated steps Docker does when exposing ports.
- Primary operation: Setting up each port mapping between host and container.
- How many times: Once for each port exposed.
Each additional port adds a similar amount of work to set up networking.
| Input Size (number of ports) | Approx. Operations |
|---|---|
| 10 | 10 setup steps |
| 100 | 100 setup steps |
| 1000 | 1000 setup steps |
Pattern observation: The work grows directly with the number of ports exposed.
Time Complexity: O(n)
This means the time to expose ports grows linearly with how many ports you expose.
[X] Wrong: "Exposing many ports happens instantly, no matter how many."
[OK] Correct: Each port requires Docker to set up networking rules, so more ports mean more work and more time.
Understanding how Docker handles port exposure helps you explain resource use and startup time clearly, a useful skill in real projects.
What if we used a range of ports instead of listing each port separately? How would the time complexity change?
Practice
What does the Docker option -p 8080:80 do when running a container?
Solution
Step 1: Understand Docker port mapping syntax
The syntax-p host_port:container_portmeans the host port is on the left and container port is on the right.Step 2: Apply to given ports
Given-p 8080:80, port 80 inside the container is accessible via port 8080 on the host.Final Answer:
It maps port 80 inside the container to port 8080 on the host machine. -> Option BQuick Check:
host_port:container_port = 8080:80 [OK]
- Confusing host and container ports order
- Thinking ports are blocked instead of mapped
- Assuming ports are exposed but not mapped
Which of the following is the correct Docker command to expose container port 5000 to host port 3000?
docker run ... myapp
Solution
Step 1: Recall port mapping syntax
The correct syntax is-p host_port:container_port.Step 2: Match ports to command
To expose container port 5000 on host port 3000, use-p 3000:5000.Final Answer:
docker run -p 3000:5000 myapp -> Option AQuick Check:
host_port:container_port = 3000:5000 [OK]
- Swapping host and container ports
- Using --expose with port mapping syntax
- Omitting container port in -p option
What will be the output when running this command and then accessing http://localhost:8080?
docker run -d -p 8080:80 nginx
Solution
Step 1: Understand the command
The command runs Nginx container detached with port 80 inside mapped to host port 8080.Step 2: Accessing localhost:8080
Since port 8080 on host maps to container's port 80 where Nginx serves, accessing localhost:8080 shows Nginx default page.Final Answer:
You will see the default Nginx welcome page in your browser. -> Option DQuick Check:
Port mapping works, Nginx serves on port 80 [OK]
- Assuming port 80 is not exposed automatically
- Expecting syntax error from correct command
- Thinking container runs but ports are closed
Identify the error in this Docker command that tries to expose port 3306:
docker run -p 3306: mysql
Solution
Step 1: Check -p option syntax
The-poption requires both host and container ports inhost_port:container_portformat.Step 2: Analyze given command
The command uses-p 3306:without specifying container port, which is invalid syntax.Final Answer:
Missing container port in the -p option. -> Option AQuick Check:
-p requires host_port:container_port [OK]
- Using only one port number with -p
- Confusing --expose and -p options
- Assuming image name causes port errors
You want to run two containers both exposing port 80 inside, but accessible on host ports 8081 and 8082 respectively. Which Docker commands correctly achieve this?
Solution
Step 1: Understand port mapping for multiple containers
Each container exposes port 80 internally. To avoid conflict, map to different host ports like 8081 and 8082.Step 2: Check each option's correctness
docker run -d -p 8081:80 app1 && docker run -d -p 8082:80 app2 correctly maps host ports 8081 and 8082 to container port 80 for app1 and app2 respectively. Others misuse port order or --expose.Final Answer:
docker run -d -p 8081:80 app1 && docker run -d -p 8082:80 app2 -> Option CQuick Check:
Host ports differ, container ports 80 mapped correctly [OK]
- Swapping host and container ports
- Using same host port for multiple containers
- Confusing --expose with port mapping
Ready to prove it?
Pass the test and this pattern moves unknown
