arrow_back
Back

Exposing Docker containers: port mapping with -p and -P

Andrew Dorokhov Andrew Dorokhov schedule 1 min read
menu_book Table of Contents

-p — explicit port mapping

docker run -d -p 8000:80 nginx
curl localhost:8000

-p 8000:80 maps host port 8000 to container port 80.

-P — publish all exposed ports

With -P, Docker picks a free host port for each EXPOSEd container port:

ID=$(docker run -d -P nginx)
docker port $ID 80
curl localhost:32771

Useful when you run many containers with exposed ports and do not want to assign host ports manually. Check assignments with docker port.

Flags reference

-P, --publish-all

Publish all container ports to the host. Docker assigns a high, free host port for each exposed port.

--expose

Like Dockerfile EXPOSE: documents ports for the container but does not publish them. Meaningful with -P or when linking containers .

code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward
Next Article

Enable Xdebug in Docker and PHP containers

arrow_forward