为什么Docker在使用-P时没有正确暴露我的端口但是使用-p 80:80?

Dan*_*eny 5 docker

我的Dockerfile包含:

EXPOSE 80
Run Code Online (Sandbox Code Playgroud)

然而,如果我运行图像,-P我无法连接到它.运行-p 80:80良好.

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:~$ 
Run Code Online (Sandbox Code Playgroud)

dcr*_*cro 6

使用时-P,docker会将公开的端口绑定到docker主机上49153到65535范围内的随机高端口.要确定实际端口,您需要运行

docker port <CONTAINER> 80
Run Code Online (Sandbox Code Playgroud)

使用时-p 80:80,将暴露端口专门绑定到主机端口80.