Note Only shows ports explicitly published with -p at run time. Ports declared with EXPOSE in the Dockerfile are not listed here unless they were also published.
docker run -p<host_port>:<container_port><image>docker run -p<host_ip>:<host_port>:<container_port><image>
Example
docker run -d-p8080:3000 myapp:1.0docker run -d-p127.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).
Frequently asked questions
How does Docker handle port mapping?
Docker covers this with 2 copy-ready snippets on this page. The "Show Port Mappings" snippet in Docker uses `docker port <container>`.
Which command does the Docker example use?
The "Show Port Mappings" snippet uses `docker port <container>`, from the Containers section of the Docker cheat sheet.
What other Docker snippets are shown for "port mapping"?
Besides "Show Port Mappings", this page also shows "Port Mapping (-p)".
Is there anything to watch out for?
Yes. For "Show Port Mappings": Only shows ports explicitly published with -p at run time. Ports declared with EXPOSE in the Dockerfile are not listed here unless they were also published.