Expose port

2 snippets in Docker

DKDocker

Port Mapping (-p)

DK · Run Options
Syntax
docker run -p <host_port>:<container_port> <image>
docker run -p <host_ip>:<host_port>:<container_port> <image>
Example
docker run -d -p 8080:3000 myapp:1.0
docker run -d -p 127.0.0.1:5432:5432 postgres:16

Note The format is always host:container. Binding to 127.0.0.1 restricts access to localhost only - critical for databases you do not want exposed on the network. Without an IP, Docker binds to 0.0.0.0 (all interfaces).

EXPOSE - Document Container Port

DK · Dockerfile
Syntax
EXPOSE <port>[/<protocol>]
Example
EXPOSE 3000
EXPOSE 8443/tcp

Note EXPOSE is documentation only - it does NOT publish the port. You still need -p at runtime. Think of it as a note to anyone reading the Dockerfile about which ports the application listens on.

Frequently asked questions

How does Docker handle expose port?
Docker covers this with 2 copy-ready snippets on this page. The "Port Mapping (-p)" snippet in Docker uses `docker run -p <host_port>:<container_port> <image>`.
Which command does the Docker example use?
The "Port Mapping (-p)" snippet uses `docker run -p <host_port>:<container_port> <image>`, from the Run Options section of the Docker cheat sheet.
What other Docker snippets are shown for "expose port"?
Besides "Port Mapping (-p)", this page also shows "EXPOSE - Document Container Port".
Is there anything to watch out for?
Yes. For "Port Mapping (-p)": The format is always host:container. Binding to 127.0.0.1 restricts access to localhost only - critical for databases you do not want exposed on the network. Without an IP, Docker binds to 0.0.0.0 (all interfaces).