docker run -d -p 8080:80 nginx and then check the port mapping with docker port <container_id>?The -p 8080:80 flag maps port 8080 on the host to port 80 inside the container. The docker port command shows this mapping as 80/tcp -> 0.0.0.0:8080.
docker run -p 127.0.0.1:8080:80 nginx, what is the difference compared to docker run -p 8080:80 nginx?Specifying 127.0.0.1:8080:80 binds the port only to localhost on the host machine, so it is not accessible from other machines on the network.
docker run -p 80:80 nginx but get an error: Bind for 0.0.0.0:80 failed: port is already allocated. What is the most likely cause?The error means the host port 80 is busy, so Docker cannot bind the container port 80 to it. You can check with sudo lsof -i :80 or similar.
You first add EXPOSE in the Dockerfile, then build the image, then run the container with port mapping, and finally verify the mapping.
Binding to 192.168.1.100 (a specific host IP) exposes the app only on that interface, reducing risk compared to binding to all interfaces (0.0.0.0). Binding to localhost (127.0.0.1) would block external access.