Containers have their own network stack. A service listening on localhost:80 inside a container isn’t reachable from the host unless you map a host port to a container port.
With Docker this is done using -p HOST_PORT:CONTAINER_PORT (e.g., -p 8080:80). Without it, curl localhost on the host will fail, even though the service is healthy inside the container.
- Docker installed and running.
 - Internet access to install packages inside the container.
 
- Start a plain Ubuntu container (no port mapping)
 
docker run -dit --name web-nomap --rm ubuntu- Install Nginx inside the container
 
docker exec web-nomap apt-get update
docker exec web-nomap apt-get install -y nginx curl- Change the default HTML
 
docker exec web-nomap bash -lc 'echo "<h1>Hello from INSIDE the container (no mapping)</h1>" > /var/www/html/index.html'- Start Nginx (foreground mode)
 
docker exec -d web-nomap nginx -g "daemon off;"
-druns the process detached so your terminal doesn’t block.
- Verify from inside the container (should work)
 
docker exec web-nomap curl -s http://localhost | head -n1Expected: "<h1>Hello from INSIDE the container (no mapping)</h1>"
- Try from the host (should FAIL)
 
curl -v http://localhostExpected: Connection refused or similar.
Why? Nginx is listening on the container’s localhost:80, which is not bound to your host network. No port mapping = no reachability from the host.
- Clean up Part 1
 
docker rm -f web-nomap- Start a new Ubuntu container with port mapping
 
docker run -dit --name web-mapped --rm -p 8080:80 ubuntu
-p 8080:80= host port 8080 → container port 80.
- Install Nginx
 
docker exec web-mapped apt-get update
docker exec web-mapped apt-get install -y nginx curl- Change the HTML
 
docker exec web-mapped bash -lc 'echo "<h1>Hello from a mapped port (8080→80)</h1>" > /var/www/html/index.html'- Start Nginx (foreground)
 
docker exec -d web-mapped nginx -g "daemon off;"- Verify from inside the container
 
docker exec web-mapped curl -s http://localhost | head -n1Expected: "<h1>Hello from a mapped port (8080→80)</h1>"
- Verify from the host (should WORK now)
 
curl -s http://localhost:8080 | head -n1Expected: "<h1>Hello from a mapped port (8080→80)</h1>"
- (Optional) See the port binding
 
docker ps --filter name=web-mapped
# or
docker inspect -f '{{json .NetworkSettings.Ports}}' web-mapped | jq- Clean up
 
docker rm -f web-mappedTo complete this lab:
- Take screenshots of the Nginx container running and the port mapping.
part1-fail.png— screenshot showing curl localhost failing from the host (no port mapping).part2-success.png— screenshot showing curl localhost:8080 succeeding from the host (with port mapping).
 - Paste the screenshots into a Google Doc.
 - Upload the document to Google Drive.
 - To submit the lab, paste the Google Drive link in the submission field in the Student Portal.
 
- Why did 
curl localhostfail in Part 1 but succeed in Part 2? - What happens if you use 
-p 80:80instead and then visithttp://localhost? - How would multiple containers avoid clashing on host ports?
 
- Nginx “not found” → forgot 
apt-get updatebeforeapt-get install. curl localhoston host still fails → confirm you started the mapped container (docker ps) and you are curling the right host port (:8080).- Nginx exits immediately → always start it with 
nginx -g "daemon off;"so it stays in the foreground (containers stop when the main process exits). 
