Recall & Review
beginner
What does the
-p flag do in a Docker run command?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?✗ Incorrect
The syntax
-p hostPort:containerPort maps host port 3000 to container port 80.Which command maps multiple ports in Docker?
✗ 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?✗ Incorrect
Docker cannot bind to a busy port and will show an error.
What is the correct order of ports in the
-p flag?✗ Incorrect
The format is
-p hostPort:containerPort.Which flag exposes a container port only inside Docker networks but not to the host?
✗ 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.