Ubuntu 16.04下如何从浏览器访问Docker容器内的服务
Hey there! Based on your setup, you’ve got a couple of straightforward ways to reach the service running inside your Docker container from your browser. Let’s break them down:
1. Use Your Local Machine’s Mapped Port (Simplest & Most Reliable)
You already added the -p 3000:3000 flag when starting your container—this does the key work of forwarding traffic from your host’s port 3000 straight to the container’s port 3000.
Just open your browser and navigate to either:
http://127.0.0.1:3000http://localhost:3000
This works because Docker automatically routes any requests hitting your host’s 3000 port to the container’s 3000 port where your service is listening.
2. Use the Container’s Direct IP Address
Since you found the container’s IP is 172.17.0.2 via docker inspect, you can also access the service directly using that address. In your browser, go to:http://172.17.0.2:3000
A quick heads-up: Container IPs from Docker’s default bridge network can change if you stop and restart the container, so this method is less consistent long-term than using the host port mapping.
Quick Troubleshooting If It’s Not Working
If neither method connects, run through these checks:
- Confirm the container is running: Execute
docker psin your host terminal—you should see yourtest-envcontainer listed with anUpstatus. - Verify the service is listening inside the container: Re-enter the container with
docker exec -it c98d858cb1a4 bash(swapc98d858cb1a4with your container’s ID/name), then runnetstat -tulpn | grep 3000orss -tulpn | grep 3000to check if the service is bound to port 3000. - Check your host firewall: If you’re using Ubuntu’s default
ufwfirewall, make sure port 3000 is allowed withsudo ufw allow 3000.
内容的提问来源于stack exchange,提问作者fwx

