树莓派Docker容器本地可访问但PC端ERR_CONNECTION_REFUSED问题排查
Hey there! Let's troubleshoot this connection issue together—since your Node.js app works fine directly on the Pi but fails when running in Docker from your PC, it's almost certainly a port binding or mapping problem. Let's walk through the most common fixes step by step:
1. Make Sure Your Node.js App Listens on 0.0.0.0 (Not localhost)
This is the #1 mistake for Docker newbies. When your app listens on localhost (127.0.0.1), it only accepts connections from inside the container itself—not from the Pi's network or your PC.
Check your Node.js code:
// ❌ Bad: Only accessible inside the container app.listen(3000, 'localhost', () => { console.log('Server running on port 3000'); }); // ✅ Good: Accepts connections from any network interface app.listen(3000, '0.0.0.0', () => { console.log('Server running on port 3000'); });
If you just use app.listen(3000) without specifying the host, some frameworks default to localhost—so explicitly setting 0.0.0.0 is safer.
2. Verify Your Docker Run Command Includes Port Mapping
Docker doesn't automatically expose container ports to the host. You need to use the -p flag to map a port on your Raspberry Pi (host) to the port your Node app uses inside the container.
For example, if your app uses port 3000:
# ✅ Correct: Maps host port 3000 to container port 3000 docker run -d -p 3000:3000 your-node-image-name # ❌ Incorrect: No port mapping—container port is not accessible outside the container docker run -d your-node-image-name
The left number is the port on your Pi, the right number is the port inside the container (must match what your Node app listens on).
3. Check Raspberry Pi Firewall Settings
If your Pi has a firewall (like ufw) enabled, it might block incoming connections to the mapped port.
- Check if the port is allowed:
sudo ufw status - If it's not listed, allow it:
sudo ufw allow 3000/tcp
4. Access the App Using Your Raspberry Pi's Local IP Address
On your PC, don't try to use localhost—that points to your own computer. Instead, find your Pi's local network IP:
On the Pi, run:
ip a
Look for the inet address under eth0 (wired) or wlan0 (wireless), e.g., 192.168.1.105.
Then on your PC's Chrome browser, go to:
http://192.168.1.105:3000
Quick Troubleshooting Check List
- Node app listens on
0.0.0.0 - Docker run command uses
-p host-port:container-port - Pi's firewall allows the host port
- PC uses Pi's local IP (not
localhost) to access
If you still have issues, feel free to share your exact Dockerfile, Node code, and run command—I can help spot any other misconfigs!
内容的提问来源于stack exchange,提问作者NeitoFR

