-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
.
Related topics
- Connecting containers — inter-container access
- Networking — bridge and host drivers
Andrew Dorokhov