0
0
Dockerdevops~5 mins

Port mapping in Compose in Docker - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is port mapping in Docker Compose?
Port mapping connects a port on your computer (host) to a port inside a Docker container, allowing you to access container services from outside.
Click to reveal answer
beginner
How do you define port mapping in a docker-compose.yml file?
Use the ports key under a service with the format host_port:container_port. For example:
ports:
  - "8080:80"
maps host port 8080 to container port 80.
Click to reveal answer
beginner
Why is port mapping important when running containers?
Containers run isolated with their own network. Port mapping lets you reach container apps from your computer or other devices by linking container ports to host ports.
Click to reveal answer
intermediate
What happens if you map the same host port to multiple containers in Compose?
You get a conflict error because only one container can use a host port at a time. You must use different host ports for each container.
Click to reveal answer
intermediate
How do you expose a container port without mapping it to a host port in Compose?
Use the expose key instead of ports. This makes the port accessible to linked containers but not to the host machine.
Click to reveal answer
In docker-compose.yml, how do you map host port 5000 to container port 80?
Aports: - "80:5000"
Bexpose: - "5000:80"
Cports: - "5000:80"
Dexpose: - "80:5000"
What does the expose key do in docker-compose?
AMakes container ports accessible only to linked containers
BMaps container ports to host ports
CBlocks all container ports
DMaps host ports to container ports
What error occurs if two services map the same host port in Compose?
ANo error, ports are shared
BPort conflict error
CContainer crash without error
DDocker ignores the second mapping
Which port is accessible from your computer when you map 8080:80?
APort 80 inside the container only
BPort 8080 inside the container
CPort 80 on your computer
DPort 8080 on your computer
If you want a container port accessible only to other containers, which should you use?
Aexpose
Bnetworks
Cvolumes
Dports
Explain how port mapping works in Docker Compose and why it is useful.
Think about how your computer talks to apps inside containers.
You got /4 concepts.
    Describe the difference between the ports and expose keys in a Compose file.
    One lets outside world in, the other only shares inside Docker.
    You got /4 concepts.