- Beginner
- Networking
How it works
The format is server:container. The server port must be free: two containers cannot publish the same one. -p 127.0.0.1:5432:5432 publishes on the loopback interface only, so only the server itself can connect. docker port lists the mappings.
What breaks
The app must listen on 0.0.0.0 inside the container: a server bound to 127.0.0.1 stays unreachable even with -p. And publishing a database port on 0.0.0.0 opens it to the internet, often past the server's firewall.
In a terminal
docker run -d --name web -p 8080:80 nginx:1.27
docker port web
curl http://localhost:8080
See it break, then fix it
Write the Dockerfile or the compose file, type the docker commands, and watch a simulated server run them through reboots, upgrades and traffic. Free to start.