Why is Docker displaying my port incorrectly when using -P, but works with -p 80:80?

My Dockerfile contains the following:

EXPOSE 80 

However, if I run the image using -P , I cannot connect to it. Working with -p 80:80 works fine.

 danny@linux :~$ sudo docker run -d -P dart-test b3277a5483531f6dc23a1c807cf895103fd5333b603c1b4a352e07c9721f1a48 # Can't connect here danny@linux :~$ curl http://localhost/ curl: (7) Failed to connect to localhost port 80: Connection refused danny@linux :~$ sudo docker stop b3277 b3277 danny@linux :~$ sudo docker run -d -p 80:80 dart-test dfe68699bfb33ce33e8e6e1953ac828b9d31209988df64e2627d9228758438ba # Connects fine here danny@linux :~$ curl http://localhost/ Hello, world! danny@linux :~$ 
+5
source share
1 answer

When you use -P , the docker binds the open port to a random high port from the range 49153 to 65535 on the docker host. To determine the actual port, you need to run

 docker port <CONTAINER> 80 

When you use -p 80:80 , you specifically bind the open port to host port 80.

+6
source

Source: https://habr.com/ru/post/1202904/


All Articles