Bird
Raised Fist0
Dockerdevops~20 mins

EXPOSE instruction for ports in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Port Exposure Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What does the EXPOSE instruction do in a Dockerfile?

Choose the correct explanation of the EXPOSE instruction in a Dockerfile.

AIt sets environment variables inside the container related to network ports.
BIt documents the ports the container listens on at runtime but does not publish them automatically.
CIt blocks all network traffic to the container except on the exposed ports.
DIt automatically publishes the specified ports to the host machine when the container runs.
Attempts:
2 left
💡 Hint

Think about whether EXPOSE opens ports by itself or just informs about them.

💻 Command Output
intermediate
2:00remaining
What is the output of 'docker port' after EXPOSE?

Given a Dockerfile with EXPOSE 8080 and a container started without port mapping, what does docker port <container_id> show?

Docker
Dockerfile:
FROM nginx
EXPOSE 8080

Commands:
docker build -t test-expose .
docker run -d --name test-container test-expose
docker port test-container
ANo output, because no ports are published to the host.
BShows 8080/tcp -> 0.0.0.0:8080, mapping container port to host port.
CShows an error because EXPOSE is not a valid command.
DShows 8080/tcp -> <container_ip>:8080, internal container mapping.
Attempts:
2 left
💡 Hint

Consider if docker run without -p or --publish publishes ports.

❓ Configuration
advanced
2:00remaining
Which Dockerfile snippet correctly exposes multiple ports?

Select the Dockerfile snippet that correctly exposes ports 80 and 443.

AEXPOSE 80 443
B
EXPOSE 80
EXPOSE 443
CEXPOSE [80, 443]
DEXPOSE "80,443"
Attempts:
2 left
💡 Hint

Think about how Dockerfile instructions handle multiple lines versus single line with multiple ports.

❓ Troubleshoot
advanced
2:00remaining
Why can't I access my container's service on the exposed port?

You have a Dockerfile with EXPOSE 5000 and run the container with docker run myapp. You try to access the service on port 5000 from your host but fail. What is the most likely reason?

ADocker automatically blocks all ports unless you disable the firewall on your host.
BThe container's firewall is blocking port 5000 internally.
CThe EXPOSE instruction does not publish ports to the host; you must use <code>-p 5000:5000</code> when running the container.
DThe EXPOSE instruction is missing the protocol, so it defaults to UDP which is not accessible.
Attempts:
2 left
💡 Hint

Remember what EXPOSE does and what is needed to make ports accessible from outside the container.

✅ Best Practice
expert
2:00remaining
What is the best practice for using EXPOSE in multi-stage Dockerfiles?

In a multi-stage Dockerfile, where should you place the EXPOSE instruction to correctly document the ports for the final image?

ADo not use EXPOSE in multi-stage Dockerfiles because it causes conflicts.
BPlace EXPOSE in every stage to document all ports used during build and runtime.
CPlace EXPOSE only in the first stage to optimize build caching.
DPlace EXPOSE only in the final stage to document ports for the final image.
Attempts:
2 left
💡 Hint

Think about which stage produces the final image used to run containers.

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