0
0
Dockerdevops~5 mins

Port mapping with -p flag in Docker - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
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
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
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
What is the correct order of ports in the -p flag?
AcontainerPort:hostPort
BhostPort:containerPort
ChostPort only
DcontainerPort only
Which flag exposes a container port only inside Docker networks but not to the host?
A-p
B-P
C--publish-all
D--expose
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.