Port mapping with -p flag in Docker - Time & Space Complexity
When using Docker's port mapping with the -p flag, it's helpful to understand how the system handles these mappings as you add more ports.
We want to see how the work grows when mapping multiple ports.
Analyze the time complexity of the following Docker command snippet.
docker run -d \
-p 8080:80 \
-p 8081:81 \
-p 8082:82 \
my-web-server
This command runs a container and maps three host ports to three container ports using the -p flag.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Processing each
-pport mapping flag. - How many times: Once per port mapping specified (here 3 times).
Each additional port mapping adds a similar amount of work to set up the mapping.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 port mappings processed |
| 100 | 100 port mappings processed |
| 1000 | 1000 port mappings processed |
Pattern observation: The work grows directly with the number of ports mapped.
Time Complexity: O(n)
This means the time to set up port mappings grows linearly with how many ports you map.
[X] Wrong: "Adding more ports with -p does not affect setup time because Docker handles it instantly."
[OK] Correct: Each port mapping requires Docker to configure networking rules, so more ports mean more work.
Understanding how Docker handles port mappings helps you explain container networking clearly and shows you can reason about resource scaling.
What if we used a range of ports with a single -p flag instead of multiple flags? How would the time complexity change?
Practice
What does the -p flag do in the docker run command?
Solution
Step 1: Understand the purpose of the
The-pflag-pflag is used to connect a port on your computer (host) to a port inside the Docker container.Step 2: Identify what port mapping means
This mapping allows you to access services running inside the container from your host machine using the specified port.Final Answer:
Maps a port on the host to a port in the container -> Option CQuick Check:
-pflag = port mapping [OK]
- Confusing -p with CPU or memory limits
- Thinking -p sets container name
- Assuming -p sets environment variables
Which of the following is the correct syntax to map host port 8080 to container port 80?
docker run -p ??? image_name
Solution
Step 1: Recall the port mapping format
The correct format is-p host_port:container_port, so the host port comes first.Step 2: Apply the ports given
Host port is 8080, container port is 80, so it should be-p 8080:80.Final Answer:
-p 8080:80 -> Option AQuick Check:
Host port first, container port second [OK]
- Swapping host and container ports
- Using dash (-) instead of colon (:)
- Using invalid port range syntax
What will happen if you run this command?docker run -p 5000:80 nginx
Then open http://localhost:5000 in your browser?
Solution
Step 1: Understand the port mapping
The command maps host port 5000 to container port 80 where Nginx serves its default page.Step 2: Accessing the service
Openinghttp://localhost:5000accesses the container's port 80 service, showing Nginx's default page.Final Answer:
You will see the default Nginx welcome page -> Option BQuick Check:
Port 5000 maps to Nginx port 80, page loads [OK]
- Thinking port 80 on host is used
- Assuming port mapping causes errors
- Not realizing Nginx serves on port 80 inside container
What is wrong with this command?docker run -p 80:8080 myapp
The app inside the container listens on port 8080.
Solution
Step 1: Check the app's listening port
The app listens on container port 8080, so container port must be 8080 in mapping.Step 2: Verify the port mapping syntax
The command maps host port 80 to container port 8080, which is correct to expose app on host port 80.Final Answer:
Host port 80 is mapped to container port 8080, which is correct -> Option AQuick Check:
Host:container = 80:8080 is valid [OK]
- Swapping host and container ports
- Assuming container port must be 80
- Confusing app listening port with host port
You want to run two containers from the same image that both expose port 80 inside the container. How can you map their ports so both are accessible on your host machine?
Solution
Step 1: Understand port conflicts on host
Two containers cannot map the same host port (like 80) simultaneously because the host port must be unique.Step 2: Assign different host ports
Map the first container's port 80 to host port 8080, and the second container's port 80 to host port 8081 to avoid conflicts.Final Answer:
Use -p 8080:80 for the first and -p 8081:80 for the second container -> Option DQuick Check:
Different host ports avoid conflicts [OK]
- Trying to map both containers to host port 80
- Swapping host and container ports
- Thinking containers can share the same host port
Ready to prove it?
Pass the test and this pattern moves unknown
