Danny Tuppeny
Danny Tuppeny

Reputation: 42413

Why does Docker not correctly expose my port when using -P but works with -p 80:80?

My Dockerfile contains this:

EXPOSE 80

Yet, if I run the image with -P I can't connect to it. Running 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:~$ 

Upvotes: 5

Views: 1581

Answers (1)

dcro
dcro

Reputation: 13679

When you use -P, docker will bind the exposed port to a random high port from the range 49153 to 65535 on the docker host. To determine the actual port, you'll need to run

docker port <CONTAINER> 80

When you use -p 80:80, you specifically bind the exposed port to the host's port 80.

Upvotes: 6

Related Questions