Back to results for “Deployment”
Node.js › Docker & Deployment

What does the EXPOSE instruction in a Dockerfile actually do, and what is a common misconception about it?

Beginner

Answer

EXPOSE is purely documentation -- it declares which port(s) the containerized application listens on, informing anyone reading the Dockerfile or using tools that introspect it, but it does not actually publish or open that port to the host machine; a common misconception is that EXPOSE alone makes a service reachable, when actually the '-p' flag on 'docker run' (or the 'ports' section in Docker Compose) is what performs the actual port mapping to the host.
# Documents that the app listens on 3000, but doesn't publish it
EXPOSE 3000

# Actual port mapping happens at run time
# docker run -p 3000:3000 myapp
Real-world example A developer confused why they couldn't reach their containerized app despite an EXPOSE 3000 line in the Dockerfile discovers they'd forgotten to add the corresponding '-p 3000:3000' flag to their 'docker run' command, which is the step that actually maps the container's port to the host.

Common follow-ups: Why does Docker Compose's 'ports' mapping still require the corresponding EXPOSE to be meaningful, or does it?;What's the difference between EXPOSE and actually publishing a port for inter-container communication within the same Docker network?