Docker容器已正常运行,但无法访问localhost:8000,求排查指导
Got it, let's walk through the most likely issues here—since your Docker containers are all running but localhost:8000 isn't loading, the problem is almost certainly tied to port mapping, Nginx configuration, network permissions, or how your app is listening inside the container. Here's what to check step by step:
1. Verify Docker Compose Port Mapping
First, double-check your docker-compose.yml to make sure your app container is actually exposing port 8000 to your host machine. You need a line like this under your app service:
services: your-app-service: # ... other config ports: - "8000:8000" # Format: host-port:container-port
If this line is missing, or the container port doesn't match what your app is using inside the container, localhost:8000 won't reach your app. To confirm what port your app is listening on inside the container, run:
docker exec <your-app-container-name> netstat -tulpn
Make sure it's listening on 0.0.0.0:8000 (not just 127.0.0.1—that would only be accessible inside the container itself).
2. Check Nginx's Role in the Setup
You mentioned accessing plain localhost (port 80) shows the Nginx default page, which means Nginx is running on your host and using port 80. Now, there are two scenarios here:
- You want to use Nginx as a reverse proxy for your app: If your goal is to have Nginx forward traffic to your app (so you access it via port 80 instead of 8000), you need to update your Nginx config to replace the default page with a proxy rule. Edit your Nginx site config (usually at
/etc/nginx/sites-available/default) to add this server block:
Then restart Nginx to apply changes:server { listen 80; server_name localhost; location / { proxy_pass http://localhost:8000; # These headers ensure your app gets correct client info proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Now accessingsudo systemctl restart nginxlocalhostshould load your app instead of the default page. - You want to access the app directly via port 8000: Ensure Nginx isn't trying to use port 8000 (check your Nginx config for any
listen 8000;lines). Also, confirm that your host's port 8000 isn't blocked (see next point).
3. Check Host Firewall Rules
Local firewalls (like ufw on Ubuntu) often block unused ports by default. To make sure port 8000 is open:
sudo ufw allow 8000/tcp sudo ufw reload
You can verify the rule is active with sudo ufw status.
4. Inspect Container Logs and App Health
Just because the container is running doesn't mean the app inside is working correctly. Check your app's container logs for errors:
docker logs <your-app-container-name>
Look for messages about failed startup, incorrect port binding, or missing dependencies. Common issues here are the app listening on 127.0.0.1 instead of 0.0.0.0 (as mentioned earlier) or configuration errors preventing the app from starting.
5. Check if Port 8000 is Occupied by Another Process
It's possible another process on your host is using port 8000, which would prevent Docker from binding to it. To check:
sudo lsof -i :8000 # Or use netstat if lsof isn't installed sudo netstat -tulpn | grep 8000
If you see a process listed, you can either stop that process or change your Docker Compose port mapping to use a different host port (e.g., 8001:8000).
内容的提问来源于stack exchange,提问作者Marc Rasmussen

