That’s true. One think to remind, with -P
flag all exposed ports inside Dockerfile
will be mapped to random ports on your host.
https://docs.docker.com/engine/reference/builder/#expose
The
EXPOSE
instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the-p
flag ondocker run
to publish and map one or more ports, or the-P
flag to publish all exposed ports and map them to high-order ports.
Remember that
-P
uses an ephemeral high-ordered host port on the host, so the port will not be the same for TCP and UDP.
And from the CLI documentation (docker help run
):
-P, --publish-all Publish all exposed ports to random ports