0
0
Dockerdevops~10 mins

EXPOSE instruction for ports in Docker - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - EXPOSE instruction for ports
Write Dockerfile with EXPOSE
Build Docker Image
Run Container
Port is marked as exposed
Optional: Map port to host
Container can accept traffic on exposed port
This flow shows how the EXPOSE instruction marks a port inside the container as open for communication, which can then be mapped to the host when running the container.
Execution Sample
Docker
FROM nginx:latest
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
This Dockerfile exposes port 80 so the container can receive web traffic on that port.
Process Table
StepActionResultNotes
1Dockerfile read: FROM nginx:latestBase image set to nginxSets base image
2Dockerfile read: EXPOSE 80Port 80 marked as exposedPort 80 is now known as open inside container
3Dockerfile read: CMD ["nginx", "-g", "daemon off;"]Default command setStarts nginx server
4Build imageImage created with exposed port 80EXPOSE info stored in image metadata
5Run container without port mappingContainer runs, port 80 exposed internallyPort 80 not accessible from host by default
6Run container with -p 8080:80Port 80 in container mapped to 8080 on hostHost can access container on localhost:8080
7Send HTTP request to localhost:8080Request reaches nginx inside containerPort mapping works
8Stop containerContainer stopsEnd of lifecycle
💡 Container stopped, port exposure and mapping demonstrated
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
EXPOSED_PORTS[][80][80][80][80]
CONTAINER_RUNNINGfalsefalsefalsetruefalse
PORT_MAPPING{}{}{}{8080:80}{8080:80}
Key Moments - 2 Insights
Does EXPOSE alone make the port accessible from the host machine?
No, EXPOSE only marks the port inside the container as open. To access it from the host, you must map the port using -p or --publish when running the container, as shown in step 6 of the execution table.
What happens if you run the container without port mapping?
The container runs and the port is exposed internally, but the host cannot access it. This is shown in step 5 where the container runs but port 80 is not accessible from outside.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the port 80 marked as exposed inside the image?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Check the 'Action' column for when EXPOSE 80 is read in the Dockerfile.
According to the variable tracker, what is the value of PORT_MAPPING after step 4?
A{8080:80}
B{80:80}
C{}
D[]
💡 Hint
Look at the PORT_MAPPING row and the After Step 4 column.
If you want to access the container's port 80 on your host's port 3000, which step in the execution table would change?
AStep 6
BStep 5
CStep 7
DStep 8
💡 Hint
Port mapping happens when running the container, see step 6.
Concept Snapshot
EXPOSE <port> in Dockerfile marks a port inside the container as open.
It does NOT publish the port to the host.
Use docker run -p hostPort:containerPort to map ports.
EXPOSE helps document which ports the container listens on.
Mapping ports enables host access to container services.
Full Transcript
The EXPOSE instruction in a Dockerfile tells Docker that the container listens on the specified network port at runtime. When building the image, this port is marked as exposed in the image metadata. However, this does not automatically make the port accessible from the host machine. To allow access, you must map the container port to a host port using the -p option when running the container. For example, running 'docker run -p 8080:80' maps container port 80 to host port 8080. Without this mapping, the port is only open inside the container and cannot be reached from outside. This visual execution showed reading the Dockerfile, building the image, running the container with and without port mapping, and how the port exposure and mapping affect accessibility.