Expose container port range dynamically and discover the mapping.
BeginnerScenarioNetworking and Compose
Answer
Situation read → Several instances on one host, avoiding port clashes.
What I'd do →
docker run -d -p 8080 myapp # random host port
docker port <container> 8080
Or -P to publish all EXPOSEd ports to random ports.
Why → EXPOSE is documentation only; -p/-P actually publishes.
Interview tip → A common trick question.
More on Networking and Compose
Containers on the default bridge cannot resolve each other by name. Why, and what do you do?BeginnerHow do you expose a container only to the local host, not the internet?BeginnerDesign a Compose file for web + api + postgres + redis with correct startup ordering.IntermediateFrontend must reach the API but not the database. Segment the network.IntermediateA container needs to call a service running on the Docker host.BeginnerWhen would you use `--network host`?Intermediate