0
0
Dockerdevops~5 mins

Exposing ports to host in Docker - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean to expose a port in Docker?
Exposing a port means making a port inside the Docker container accessible from the host machine or other containers. It allows communication between the container and the outside world.
Click to reveal answer
beginner
Which Docker command option is used to expose a container port to the host?
The -p or --publish option is used with docker run to map a container port to a host port, like -p 8080:80.
Click to reveal answer
beginner
Explain the syntax -p 8080:80 in Docker port mapping.
It means map port 80 inside the container to port 8080 on the host. So, accessing localhost:8080 on the host reaches port 80 in the container.
Click to reveal answer
intermediate
What is the difference between EXPOSE in Dockerfile and -p in docker run?
EXPOSE in Dockerfile is a hint that the container listens on a port but does not publish it. -p in docker run actually maps the port to the host.
Click to reveal answer
beginner
Can you expose multiple ports from a container to the host? How?
Yes, by using multiple -p options in docker run, for example: -p 8080:80 -p 8443:443 to expose HTTP and HTTPS ports.
Click to reveal answer
Which Docker command option exposes a container port to the host?
A-p
B-e
C-v
D-d
What does -p 3000:3000 do in docker run?
AMaps container port 3000 to host port 80
BRuns container in detached mode
CExposes port 80 inside the container
DMaps host port 3000 to container port 3000
What is the purpose of the EXPOSE instruction in a Dockerfile?
ATo publish ports to the host
BTo document which ports the container listens on
CTo start the container
DTo set environment variables
How do you expose multiple ports from a container?
AUse -e option multiple times
BUse multiple EXPOSE lines in Dockerfile only
CUse multiple -p options in docker run
DUse -d option multiple times
If you run docker run -p 8080:80, how do you access the container's service?
ABy visiting localhost:8080 on the host
BBy visiting localhost:80 on the host
CBy visiting container IP:8080
DBy visiting container IP:80
Describe how to expose a container port to the host using Docker commands.
Think about how to connect container ports to your computer ports.
You got /3 concepts.
    Explain the difference between EXPOSE in Dockerfile and the -p option in docker run.
    One is for documentation, the other for actual port access.
    You got /3 concepts.