Bird
Raised Fist0
Dockerdevops~5 mins

EXPOSE instruction for ports in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you run an app inside a container, it often listens on a port. The EXPOSE instruction in a Dockerfile tells Docker which port the app will use, so you can connect to it from outside the container.
When you want to tell Docker which port your app inside the container listens on.
When you plan to map container ports to your computer or server ports.
When you want to document the ports your containerized app uses for others.
When you build a Docker image that will run a web server or API service.
When you want to make sure your container’s network traffic is accessible.
Config File - Dockerfile
Dockerfile
FROM nginx:1.23.4
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

FROM nginx:1.23.4 sets the base image to nginx web server version 1.23.4.

EXPOSE 80 tells Docker that the container listens on port 80.

CMD runs nginx in the foreground so the container stays alive.

Commands
Builds a Docker image named 'my-nginx-app' from the Dockerfile in the current folder.
Terminal
docker build -t my-nginx-app .
Expected OutputExpected
Sending build context to Docker daemon 2.048kB Step 1/3 : FROM nginx:1.23.4 ---> 4bb46517cac3 Step 2/3 : EXPOSE 80 ---> Using cache ---> 7f3b1a9a5f3e Step 3/3 : CMD ["nginx", "-g", "daemon off;"] ---> Using cache ---> 9c1a2f3b4d5e Successfully built 9c1a2f3b4d5e Successfully tagged my-nginx-app:latest
Runs the container in detached mode, mapping your computer's port 8080 to the container's port 80 so you can access the web server.
Terminal
docker run -d -p 8080:80 my-nginx-app
Expected OutputExpected
e3b1a2c4d5f6a7b8c9d0e1f2a3b4c5d6e7f8g9h0i1j2k3l4m5n6o7p8q9r0s1t2
→
-d - Run container in background (detached mode)
→
-p 8080:80 - Map host port 8080 to container port 80
Checks if the nginx web server inside the container is reachable through port 8080 on your computer.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
Key Concept

EXPOSE in a Dockerfile declares the port your app listens on, helping Docker and users know how to connect to your container.

Common Mistakes
Not using EXPOSE in the Dockerfile and expecting the port to be known automatically.
Docker does not know which ports your app uses without EXPOSE, so documentation and some tools may not work properly.
Always add EXPOSE with the correct port number your app listens on.
Running the container without mapping ports using -p flag.
Even if EXPOSE is set, the container port is not accessible from your computer unless you map it with -p.
Use docker run -p hostPort:containerPort to access the app from outside.
Mapping the wrong host port to container port.
If you map to a port already in use or the wrong port, your app won't be reachable as expected.
Check which ports are free on your host and map correctly, e.g., -p 8080:80.
Summary
EXPOSE in Dockerfile tells Docker which port the app inside listens on.
Build the image with docker build and run it with docker run -p to map ports.
Use curl or a browser to check the app is reachable on the mapped port.

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

  1. Step 1: Understand the role of EXPOSE

    The EXPOSE instruction tells Docker which ports the container will listen on internally.
  2. Step 2: Clarify what EXPOSE does not do

    EXPOSE does not publish or map ports to the host automatically; it only documents them.
  3. Final Answer:

    To document which ports the container listens on -> Option C
  4. 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

  1. Step 1: Recall Dockerfile EXPOSE syntax

    The correct syntax is simply EXPOSE followed by the port number without any punctuation.
  2. Step 2: Check each option

    Options A, B, and C use invalid syntax with brackets, colons, or parentheses which are not allowed.
  3. Final Answer:

    EXPOSE 8080 -> Option A
  4. 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

  1. Step 1: Identify all EXPOSE instructions

    The Dockerfile exposes port 80 and port 443 explicitly.
  2. Step 2: Understand multiple EXPOSE usage

    Multiple EXPOSE lines add ports cumulatively, so both ports are exposed internally.
  3. Final Answer:

    Ports 80 and 443 -> Option A
  4. Quick Check:

    Multiple EXPOSE lines add ports together [OK]
Hint: Multiple EXPOSE lines add ports cumulatively [OK]
Common Mistakes:
  • Assuming only the last EXPOSE counts
  • Thinking EXPOSE publishes ports to host
  • Ignoring multiple EXPOSE instructions
4. You wrote this Dockerfile snippet:
EXPOSE 80 443

But Docker build gives an error. What is wrong?
medium
A. Port numbers must be in quotes
B. Ports must be separated by commas
C. EXPOSE must be lowercase
D. EXPOSE cannot list multiple ports on one line

Solution

  1. Step 1: Check EXPOSE syntax rules

    EXPOSE accepts multiple ports on one line separated by spaces; it does not require multiple EXPOSE lines.
  2. Step 2: Validate other options

    Ports do not need quotes, EXPOSE is uppercase by convention, and commas are not used to separate ports.
  3. Final Answer:

    EXPOSE cannot list multiple ports on one line -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Dockerfile: EXPOSE 3000 and run with -p 8080:3000 -> Option B
  4. Quick Check:

    EXPOSE internal port, map host:container ports correctly [OK]
Hint: EXPOSE internal port; map host port to container port with -p [OK]
Common Mistakes:
  • Exposing wrong port inside Dockerfile
  • Reversing host and container ports in -p
  • Listing multiple ports in one EXPOSE line
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard