Card 03/ 06
All 6 cards
GotchaDifficulty: Beginner1 min
Which Number in `-p 8080:80` Is Yours
The container is running, docker ps says so, and the browser says the connection was refused. Nothing is broken. The two numbers are the wrong way round.
$ docker run -d -p 80:8080 nginx
$ curl localhost:8080
curl: (7) Failed to connect to localhost port 8080 after 0 ms: Couldn't connect to server-p reads host first, container second. The command above asked the host to listen on 80 and forward to port 8080 inside the container, where nothing is listening, because nginx serves on 80.
-p 8080:80 | Means |
|---|---|
8080 | The port on your machine. This is the one you type in the browser |
80 | The port the program inside the container is listening on |
Reversed, it usually fails quietly rather than loudly: the host really does open the port you named, so docker ps looks correct and only the connection fails.