Course contentsShow
← Cloud and Infrastructure
Lesson 1007 of 2,98920. Docker in DepthFree lesson

EXPOSE: Port Documentation

Understand EXPOSE instruction for documenting container ports and its relationship with port publishing.

EXPOSE: Port Documentation

You'll learn what the EXPOSE instruction does in a Dockerfile and why it's important for container communication documentation.

What Is EXPOSE?

The EXPOSE instruction in a Dockerfile documents which network ports your container will listen on at runtime. Think of it like posting a sign on your apartment door that says "Deliveries accepted at side entrance, door 8080"—it doesn't actually unlock the door, but it tells visitors where to knock.

Here's the crucial point: EXPOSE does not publish or open ports automatically. It's purely metadata that serves as documentation between the image builder and whoever runs the container.

Syntax

EXPOSE 8080
EXPOSE 8080/tcp
EXPOSE 53/udp

You can specify the protocol (TCP is default) and list multiple ports on separate lines or space-separated.

Why Use EXPOSE?

  1. Documentation: Other developers (including future you) can see which ports the application uses
  2. Tool Integration: Tools like docker-compose and orchestrators can read EXPOSE to configure networking automatically
  3. Random Port Publishing: When you run docker container run -P (capital P), Docker will publish all EXPOSEd ports to random host ports

EXPOSE vs Actual Port Publishing

EXPOSE is not the same as the -p flag when running containers. To actually make a port accessible from outside the container, you must use -p or -P at runtime:

docker container run -p 8080:8080 myapp

Think of EXPOSE as the blueprint annotation, while -p is the actual construction work.

Key Takeaway:
EXPOSE documents which ports your container listens on but doesn't publish them—it's metadata for humans and tools, not a networking command.