Windows 10环境下Docker容器MongoDB无法通过Compass连接怎么办?
Let’s work through this problem step by step—there are a few common culprits here, and we’ll rule them out one by one:
1. Verify Port Mapping is Working Correctly
First, let’s confirm Docker is actually forwarding traffic from your Windows host’s port 28000 to the container’s 27017 port.
- Run
docker psin your command prompt. Look for thePORTScolumn for yourmongodbcontainer. It should show something like0.0.0.0:28000->27017/tcpor:::28000->27017/tcp. If it shows127.0.0.1:28000->27017/tcp, that means the port is only bound to your Docker host’s loopback interface, not accessible from your Windows machine’s network. - On your Windows machine, run
netstat -ano | findstr :28000to check if the port is being listened on. You should see a line withLISTENINGand a PID—this PID should match the Docker process (confirm with Task Manager).
2. Ensure MongoDB is Bound to All IP Addresses
By default, MongoDB 3.4 binds only to 127.0.0.1 (loopback) inside the container, which means it won’t accept connections from outside the container—even if the port is mapped.
To fix this, start the MongoDB container with the --bind_ip_all flag to allow connections from any IP:
# First stop and remove the existing container (if you don't have data to preserve) docker stop mongodb docker rm mongodb # Start a new container with the bind IP flag docker container run -p 28000:27017 --name mongodb --detach mongo:3.4 --bind_ip_all
If you have data to keep, you can either:
- Use a Docker volume to persist data (recommended for future use), or
- Exec into the container, modify the
mongod.conffile to setbindIp: 0.0.0.0, then restart the MongoDB service inside the container.
3. Check Windows Firewall Settings
Windows Firewall might be blocking incoming traffic on port 28000. Here’s how to fix it:
- Open Windows Defender Firewall > Advanced Settings > Inbound Rules.
- Click New Rule... and select Port as the rule type.
- Choose TCP, enter
28000as the specific local port, then click Next. - Select Allow the connection, choose when the rule applies (Domain, Private, Public—adjust based on your network), name the rule, and save it.
You can also temporarily disable the firewall to test if it’s the issue (remember to re-enable it afterward!).
4. Validate Your Compass Connection String
Make sure you’re using the correct connection string in MongoDB Compass:
- Use
mongodb://<your_windows_ip>:28000(replace<your_windows_ip>with your actual 192.168.x.x address). - Since you didn’t set up authentication when starting the container, leave the username/password fields blank.
- Avoid using
localhostin the connection string—use your actual Windows IP instead, aslocalhostin Compass refers to your Windows machine, but we need to target the port Docker is forwarding.
5. Test Connectivity with a Simple Tool
If Compass still isn’t working, test the connection with PowerShell’s Test-NetConnection to rule out Compass-specific issues:
Test-NetConnection <your_windows_ip> -Port 28000
If you see TcpTestSucceeded : True, the port is reachable—so the issue is likely with Compass settings. If not, go back to steps 1-3.
内容的提问来源于stack exchange,提问作者Steephen

