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.
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 "connection refused" 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.
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/udpfor 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 -Ppublishes every exposed port to a random high host port.
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:8080maps host port to container port. - Random mapping:
-Puses the exposed ports and picks free host ports;docker portshows the result. - Restrict the interface:
-p 127.0.0.1:8080:8080keeps it local to the host.
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 psPORTS column ordocker port <container>. - Real listener:
docker exec <container> ss -ltn(ornetstat -ltn) shows what the process is bound to; EXPOSE can name a port nothing listens on. - Compose:
expose:is equivalent metadata, whileports:publishes.
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:
-pbinds0.0.0.0by default and Docker writes its own iptables rules, so host firewall tools such as ufw may not block it. Publish to127.0.0.1or use a reverse proxy. - Kubernetes: EXPOSE is ignored; use a Service for access, and treat
containerPortmostly as documentation.
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
ARGorENVfor configurable ports and EXPOSE the default. - Publish deliberately: prefer explicit
-p host:containerand 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.
- 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.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Believing EXPOSE makes the port reachable from the host Only -p or -P publishes a port; EXPOSE is metadata.
- 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.
- 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.
- Letting EXPOSE drift from the application's real port Nothing validates it, so misleading metadata sends operators and tooling to the wrong port.