Sub topic 6
Letting Docker choose host ports with -P
This chapter assumes you can run docker run, know what an image and a container are, and understand TCP ports and localhost. You should also know that -p HOST:CONTAINER publishes one port, as covered earlier in this tutorial.
Why let Docker choose
With -p 8080:80, you pick the host port. That fails if another process already uses 8080. It also gets awkward when you start many containers from the same image, because each one needs a different host port.
The -P flag (capital P) avoids both problems. It publishes every port the image declares with EXPOSE, and it maps each one to a random free port on the host. Docker picks from the ephemeral range, which is usually 32768 and above on Linux.
How -P works
-P depends on the image's EXPOSE instructions. The official nginx image contains EXPOSE 80, so -P publishes port 80.
docker run -d --name web-auto -P nginx
docker port web-autoThe docker port command lists the mappings for the container. The output looks like this, though your host port will differ:
80/tcp -> 0.0.0.0:32768
80/tcp -> [::]:32768The first line is the IPv4 mapping and the second is the IPv6 mapping. Here the nginx welcome page is reachable at http://localhost:32768.
The sequence below shows what happens.
Reading the port in a script
The host port changes on every run, so a script must ask Docker for it. Pass the container port to docker port to get only the matching lines:
docker run -d --name web-script -P nginx
docker port web-script 80/tcpYou can also read the mapping with docker inspect and a Go template:
docker inspect --format '{{ (index (index .NetworkSettings.Ports "80/tcp") 0).HostPort }}' web-scriptThis prints one number, such as 32769, which you can store in a shell variable and use with curl.
Mixing -P and -p
You can use both flags in one command. An explicit -p mapping is applied as written, and -P publishes the remaining exposed ports. If you only need to fix one port and let Docker choose the rest, this is the simplest form.
Common errors
docker portprints nothing. The image has noEXPOSEinstruction, so-Phad nothing to publish. Use-pinstead, or build an image that declares the port.- The host port changed after a restart. Automatic ports are not guaranteed to stay the same when a container is recreated. Use
-pwhen clients need a fixed address. - The port is published but the connection fails.
-Ponly creates the mapping. The application inside the container must still listen on an address other than127.0.0.1, which a later chapter on troubleshooting covers. - The service is reachable from other machines. By default
-Pbinds to all host addresses.-Phas no host-IP option, so use-p 127.0.0.1::80when you need loopback only. Leaving the host port empty, as in that form, still lets Docker choose it.
Cleaning up
Containers started with -P keep their ports reserved until they stop. Remove the examples when you finish:
docker rm -f web-auto web-scriptExercise
You run docker run -d --name api -P myimage, but docker port api prints nothing. What is the most likely cause, and how can you still publish port 3000?
Show answer
The image has no EXPOSE instruction, so -P has no ports to publish. Publish the port explicitly with -p, for example docker run -d --name api -p 3000:3000 myimage, or use -p 3000 to let Docker choose the host port.
Exercise
Write a command that starts nginx, lets Docker choose the host port, and binds that port to loopback only. Then write the command that shows the chosen port.
Show answer
docker run -d --name web-lo -p 127.0.0.1::80 nginx followed by docker port web-lo 80/tcp.