The -p flag maps a port on your computer (host) to a port inside the Docker container. This lets you access the container's service from outside.
Click to reveal answer
beginner
How do you map port 8080 on your computer to port 80 inside a Docker container?
Use docker run -p 8080:80 <image-name>. This means requests to port 8080 on your computer go to port 80 in the container.
Click to reveal answer
intermediate
Can you map multiple ports using the -p flag?
Yes, you can use multiple -p flags to map several ports, like -p 8080:80 -p 443:443.
Click to reveal answer
intermediate
What happens if you try to map a host port that is already in use?
Docker will give an error because the port is busy. You must choose a free port on your computer to map.
Click to reveal answer
advanced
Explain the difference between -p and --expose in Docker.
-p maps container ports to host ports for external access. --expose only makes ports available inside Docker networks but not to the host.
Click to reveal answer
What does docker run -p 3000:80 myapp do?
AMaps host port 3000 to container port 80
BMaps container port 3000 to host port 80
CRuns container on port 3000 only
DExposes port 80 inside the container only
✗ Incorrect
The syntax -p hostPort:containerPort maps host port 3000 to container port 80.
Which command maps multiple ports in Docker?
Adocker run -p 80:80,443:443 myapp
Bdocker run -port 80 443 myapp
Cdocker run -p 80:80 -p 443:443 myapp
Ddocker run --expose 80 443 myapp
✗ Incorrect
Use multiple -p flags to map multiple ports.
If port 8080 on your computer is busy, what happens when you run docker run -p 8080:80 myapp?
ADocker shows an error and stops
BDocker runs but port 8080 is ignored
CDocker automatically picks a free port
DDocker maps to a random port
✗ Incorrect
Docker cannot bind to a busy port and will show an error.
What is the correct order of ports in the -p flag?
AcontainerPort:hostPort
BhostPort:containerPort
ChostPort only
DcontainerPort only
✗ Incorrect
The format is -p hostPort:containerPort.
Which flag exposes a container port only inside Docker networks but not to the host?
A-p
B-P
C--publish-all
D--expose
✗ Incorrect
--expose makes ports available inside Docker networks but does not map them to the host.
Explain how port mapping works in Docker using the -p flag.
Think about how your computer talks to the container through ports.
You got /4 concepts.
Describe what happens if you try to map a host port that is already in use by another program.
Consider what your computer does when two apps want the same door.
You got /3 concepts.
Practice
(1/5)
1.
What does the -p flag do in the docker run command?
easy
A. Names the container
B. Sets the container's CPU limit
C. Maps a port on the host to a port in the container
D. Specifies the container's environment variables
Solution
Step 1: Understand the purpose of the -p flag
The -p flag 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 C
Quick Check:
-p flag = port mapping [OK]
Hint: Remember: -p connects host port to container port [OK]
Common Mistakes:
Confusing -p with CPU or memory limits
Thinking -p sets container name
Assuming -p sets environment variables
2.
Which of the following is the correct syntax to map host port 8080 to container port 80?
docker run -p ??? image_name
easy
A. -p 8080:80
B. -p 80:8080
C. -p 8080-80
D. -p 80-8080
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 A
Quick Check:
Host port first, container port second [OK]
Hint: Host port comes before container port in -p [OK]
Common Mistakes:
Swapping host and container ports
Using dash (-) instead of colon (:)
Using invalid port range syntax
3.
What will happen if you run this command? docker run -p 5000:80 nginx Then open http://localhost:5000 in your browser?
medium
A. Nothing happens because port 5000 is not mapped
B. You will see the default Nginx welcome page
C. You will see a Docker error about port mapping
D. Connection refused error because port 80 is blocked
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
Opening http://localhost:5000 accesses the container's port 80 service, showing Nginx's default page.
Final Answer:
You will see the default Nginx welcome page -> Option B
Quick Check:
Port 5000 maps to Nginx port 80, page loads [OK]
Hint: Host port maps to container port, access via host port [OK]
Common Mistakes:
Thinking port 80 on host is used
Assuming port mapping causes errors
Not realizing Nginx serves on port 80 inside container
4.
What is wrong with this command? docker run -p 80:8080 myapp
The app inside the container listens on port 8080.
medium
A. Host port 80 is mapped to container port 8080, which is correct
B. The ports are reversed; it should be -p 8080:80
C. The container port should be 80 if the app listens on 8080
D. Nothing is wrong; this command works as expected
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 A
Quick Check:
Host:container = 80:8080 is valid [OK]
Hint: Remember: host port first, container port second [OK]
Common Mistakes:
Swapping host and container ports
Assuming container port must be 80
Confusing app listening port with host port
5.
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?
hard
A. You cannot run two containers exposing the same port
B. Use -p 80:80 for both containers
C. Use -p 80:8080 for the first and -p 80:8081 for the second
D. Use -p 8080:80 for the first and -p 8081:80 for the second container
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 D
Quick Check:
Different host ports avoid conflicts [OK]
Hint: Use different host ports for multiple containers [OK]
Common Mistakes:
Trying to map both containers to host port 80
Swapping host and container ports
Thinking containers can share the same host port
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.