Using the EXPOSE Instruction to Open Ports in Docker
📖 Scenario: You are creating a Docker image for a simple web application. To allow the application to communicate with the outside world, you need to open a specific port inside the container.
🎯 Goal: Learn how to use the EXPOSE instruction in a Dockerfile to open a port for your application.
📋 What You'll Learn
Create a Dockerfile with a base image
Add the EXPOSE instruction for a specific port
Build the Docker image
Run the container and verify the port is exposed
💡 Why This Matters
🌍 Real World
Opening ports in Docker containers is essential to allow applications inside containers to communicate with users or other services.
💼 Career
Understanding how to expose ports is a fundamental skill for DevOps engineers and developers working with containerized applications.
Progress0 / 4 steps
1
Create a Dockerfile with a base image
Create a file named Dockerfile and add a line to use the base image nginx:latest.
Docker
Hint
The first line in a Dockerfile usually starts with FROM followed by the image name.
2
Add the EXPOSE instruction for port 80
Add a line to the Dockerfile to expose port 80 using the EXPOSE instruction.
Docker
Hint
Use EXPOSE 80 to tell Docker to open port 80 inside the container.
3
Build the Docker image
Run the command docker build -t my-nginx . in your terminal to build the Docker image with the tag my-nginx.
Docker
Hint
Open your terminal and run docker build -t my-nginx . to create the image.
4
Run the container and verify the port is exposed
Run the container with docker run -d -p 8080:80 --name webserver my-nginx and then run docker ps to check that port 8080 on your machine maps to port 80 in the container.
Docker
Hint
Use docker run -d -p 8080:80 --name webserver my-nginx to start the container and docker ps to see the port mapping.
Practice
(1/5)
1. What is the main purpose of the EXPOSE instruction in a Dockerfile?
easy
A. To start the container with a specific port
B. To automatically publish ports to the host machine
C. To document which ports the container listens on
D. To stop the container from using any ports
Solution
Step 1: Understand the role of EXPOSE
The EXPOSE instruction tells Docker which ports the container will listen on internally.
Step 2: Clarify what EXPOSE does not do
EXPOSE does not publish or map ports to the host automatically; it only documents them.
Final Answer:
To document which ports the container listens on -> Option C
Quick Check:
EXPOSE documents ports, does not publish them [OK]
Hint: EXPOSE documents ports, doesn't publish them automatically [OK]
Common Mistakes:
Thinking EXPOSE publishes ports to host
Confusing EXPOSE with port mapping
Assuming EXPOSE starts container on a port
2. Which of the following is the correct syntax to expose port 8080 in a Dockerfile?
easy
A. EXPOSE 8080
B. EXPOSE:8080
C. EXPOSE(port=8080)
D. EXPOSE[8080]
Solution
Step 1: Recall Dockerfile EXPOSE syntax
The correct syntax is simply EXPOSE followed by the port number without any punctuation.
Step 2: Check each option
Options A, B, and C use invalid syntax with brackets, colons, or parentheses which are not allowed.
Final Answer:
EXPOSE 8080 -> Option A
Quick Check:
Correct syntax is EXPOSE followed by port number [OK]
Hint: EXPOSE followed by space and port number is correct [OK]
Common Mistakes:
Using colons or parentheses in EXPOSE
Adding brackets around port number
Writing EXPOSE in lowercase
3. Given this Dockerfile snippet:
FROM nginx
EXPOSE 80
EXPOSE 443
What ports will the container listen on internally?
medium
A. Ports 80 and 443
B. Only port 443
C. Only port 80
D. No ports are exposed
Solution
Step 1: Identify all EXPOSE instructions
The Dockerfile exposes port 80 and port 443 explicitly.
Step 2: Understand multiple EXPOSE usage
Multiple EXPOSE lines add ports cumulatively, so both ports are exposed internally.
EXPOSE accepts multiple ports on one line separated by spaces; it does not require multiple EXPOSE lines.
Step 2: Validate other options
Ports do not need quotes, EXPOSE is uppercase by convention, and commas are not used to separate ports.
Final Answer:
EXPOSE cannot list multiple ports on one line -> Option D
Quick Check:
One port per EXPOSE line only [OK]
Hint: Use one EXPOSE per port, no spaces or commas [OK]
Common Mistakes:
Listing multiple ports on one EXPOSE line
Adding quotes around port numbers
Writing EXPOSE in lowercase
5. You want to run a web app container that listens on port 3000 inside the container. You want to access it on port 8080 on your host machine. Which Docker commands and Dockerfile instructions correctly achieve this?
hard
A. Dockerfile: EXPOSE 8080 Run: docker run -p 3000:8080 image
B. Dockerfile: EXPOSE 3000 Run: docker run -p 8080:3000 image
C. Dockerfile: EXPOSE 3000 8080 Run: docker run -p 3000:8080 image
D. Dockerfile: EXPOSE 8080 Run: docker run -p 8080:8080 image
Solution
Step 1: Understand EXPOSE and port mapping
EXPOSE should list the internal container port (3000). The run command maps host port 8080 to container port 3000.
Step 2: Check each option's correctness
Dockerfile: EXPOSE 3000 Run: docker run -p 8080:3000 image correctly exposes 3000 and maps 8080 on host to 3000 in container. Others either reverse ports or use invalid EXPOSE syntax.
Final Answer:
Dockerfile: EXPOSE 3000 and run with -p 8080:3000 -> Option B