⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 33 of 159 in Docker & Containers
Senior DevOps / SRE [L2] Docker Production Scenario [L2]
🎯 Target Role / Context: Docker Scenario #33 (L2) · Docker

Q: What does `EXPOSE` do in a Dockerfile?

EXPOSE in a Dockerfile records which port the container is expected to listen on as image metadata; it does not open or publish that port to the host. Publishing happens at run time with -p or -P.

#Docker #L2 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
"<strong><code>EXPOSE</code> in a Dockerfile</strong> records which port the container is expected to listen on as image metadata; it does <em>not</em> open or publish that port to the host. Publishing happens at run time with <code>-p</code> or <code>-P</code>. This is a classic interview trap, because many candidates say EXPOSE makes a port reachable. Knowing the distinction helps you debug &quot;connection refused&quot; problems, understand what tools such as <code>docker run -P</code> and some reverse-proxy integrations read, and avoid accidentally exposing services to the network."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: <strong><code>EXPOSE</code> in a Dockerfile</strong> records which port the container is expected to listen on as image metadata; it does <em>not</em> open or publish that port to the host. Publishing happens at run time with <code>-p</code> or <code>-P</code>. This is a classic interview trap, because many candidates say EXPOSE makes a port reachable. Knowing the distinction helps you debug &quot;connection refused&quot; problems, understand what tools such as <code>docker run -P</code> and some reverse-proxy integrations read, and avoid accidentally exposing services to the network.

1️⃣

What EXPOSE Actually Does

It adds an entry to the image config under ExposedPorts, and nothing else.

  • Documentation: it tells the reader and tooling which port the app uses, like a comment that can be queried.
  • Protocol: default is TCP; write EXPOSE 53/udp for UDP, or list both.
  • No networking effect: containers on the same user-defined network can reach each other on any listening port, whether or not it is exposed.
  • Used by -P: docker run -P publishes every exposed port to a random high host port.
2️⃣

Compare EXPOSE and Publishing in Practice

Run the same image with and without publishing to see the difference.

# Dockerfile
FROM nginx:1.27-alpine
EXPOSE 80/tcp

docker build -t my-web:1.0 .
docker run -d --name web1 my-web:1.0          # reachable only inside Docker networks
docker run -d --name web2 -p 8080:80 my-web:1.0
docker run -d --name web3 -P my-web:1.0
docker port web3
# 80/tcp -> 0.0.0.0:32768

curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080   # 200
  • Only EXPOSE: the host cannot reach the app by localhost:8080.
  • Explicit publish: -p 8080:8080 maps host port to container port.
  • Random mapping: -P uses the exposed ports and picks free host ports; docker port shows the result.
  • Restrict the interface: -p 127.0.0.1:8080:8080 keeps it local to the host.
Advertisement
3️⃣

Verify Exposed and Published Ports

Check metadata, runtime mappings and the real listener separately.

docker inspect --format '{{json .Config.ExposedPorts}}' my-web:1.0
# {"80/tcp":{}}
docker ps --format 'table {{.Names}}\t{{.Ports}}'
docker exec web2 ss -ltn
  • Image metadata: docker inspect --format '{{json .Config.ExposedPorts}}' my-web:1.0.
  • Published mappings: docker ps PORTS column or docker port <container>.
  • Real listener: docker exec <container> ss -ltn (or netstat -ltn) shows what the process is bound to; EXPOSE can name a port nothing listens on.
  • Compose: expose: is equivalent metadata, while ports: publishes.
4️⃣

Failure Modes and Security Surprises

Most problems come from the bind address or from assuming EXPOSE publishes.

  • Bound to 127.0.0.1 inside the container: a published port forwards to the container's network interface, so an app listening only on loopback is unreachable. Bind to 0.0.0.0.
  • Port mismatch: EXPOSE says 3000 but the app listens on 8080; the metadata is not enforced.
  • Firewall bypass: -p binds 0.0.0.0 by default and Docker writes its own iptables rules, so host firewall tools such as ufw may not block it. Publish to 127.0.0.1 or use a reverse proxy.
  • Kubernetes: EXPOSE is ignored; use a Service for access, and treat containerPort mostly as documentation.
Pro Tip: Seeing the page work from another container but not from your laptop almost always means the port was never published with <code>-p</code>, regardless of EXPOSE.
5️⃣

Use EXPOSE as Good Practice, Not Security

Keep the instruction accurate and let run-time configuration control access.

  • Declare the real port: keep it in sync with the app's default; use an ARG or ENV for configurable ports and EXPOSE the default.
  • Publish deliberately: prefer explicit -p host:container and bind to a specific interface in production.
  • Review in CI: a linter such as hadolint plus an inspect assertion can verify the metadata.
  • Least exposure: expose only application ports, never debug or admin ports, in the image.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Treat EXPOSE as documentation and <code>-p</code> as the real switch; when a published port refuses connections, check whether the app listens on <code>0.0.0.0</code> rather than <code>127.0.0.1</code>."
⚡ 60-Second Elevator Pitch Talking Points
  • EXPOSE records a port in the image metadata; it does not publish it or change any firewall behaviour.
  • Publishing happens with -p or -P, and I verify with docker port plus ss inside the container to see the real listener.
  • I bind published ports to a specific interface in production and make sure the app listens on 0.0.0.0, not loopback.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Believing EXPOSE makes the port reachable from the host Only -p or -P publishes a port; EXPOSE is metadata.
  2. Running the app bound to 127.0.0.1 in the container Traffic arriving through a published port comes via the container's interface, so loopback-only apps refuse it.
  3. Using -p 5432:5432 for a database on a public host It listens on all interfaces and Docker's iptables rules can bypass the host firewall; bind to 127.0.0.1 or use a private network.
  4. Letting EXPOSE drift from the application's real port Nothing validates it, so misleading metadata sends operators and tooling to the wrong port.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →