Docker Toolbox环境下RabbitMQ连接拒绝问题求助
Let’s break down why this is happening and how to fix it—since you’re on Windows Home, Docker Toolbox relies on a VirtualBox VM instead of WSL2 (only available on Pro/Edu editions), and that’s where the port mapping got tripped up.
The Root Cause
You can access the management UI at 192.168.99.101:15672 because that port was mapped correctly when starting the RabbitMQ container, but the AMQP port 5672 (the one your code uses) is likely not being forwarded from the VM to your Windows host. Here’s how to fix it step by step:
Step 1: Verify RabbitMQ Container Port Mapping
First, check if your container was started with the 5672 port mapped. Run this command in your Docker Toolbox terminal:
docker ps
Look for the PORTS column for your rabbitmq container. You should see something like 0.0.0.0:5672->5672/tcp, 0.0.0.0:15672->15672/tcp. If 5672->5672 is missing, re-run the container with the correct port mapping:
# Stop and remove the existing container (if needed) docker stop rabbitmq && docker rm rabbitmq # Start a new container with both ports mapped docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
Step 2: Check VirtualBox Port Forwarding Rules
Docker Toolbox uses a VirtualBox VM named default to host containers. Sometimes port mapping doesn’t propagate correctly, so manually confirm:
- Open VirtualBox Manager
- Select the
defaultVM, then click Settings > Network > Advanced > Port Forwarding - Look for a rule for port 5672. If it’s missing, add a new rule:
- Name:
rabbitmq-amqp - Protocol:
TCP - Host Port:
5672 - Guest Port:
5672 - Leave Host IP and Guest IP blank
- Name:
- Save settings and restart the
defaultVM (right-click > Start > Normal Start)
Step 3: Update Your Code’s Connection Settings
Now that the port is properly mapped, update your setSpecificationFactory method to use the VM’s IP (192.168.99.101) instead of localhost or 127.0.0.1—on Windows Home, localhost points to your Windows host, not the Docker VM:
private void setSpecificationFactory(ConnectionFactory factory){ factory.setUsername("guest"); factory.setPassword("guest"); factory.setVirtualHost("/"); factory.setHost("192.168.99.101"); // Use the VM's IP here factory.setPort(5672); }
Step 4: Verify the Port is Accessible
Before running your code, test if the port is reachable from Windows:
- Open PowerShell and run:
Look forTest-NetConnection 192.168.99.101 -Port 5672TcpTestSucceeded : Truein the output—if that’s there, your code should connect successfully.
Bonus: Check RabbitMQ Internal Listening
If the above steps don’t work, confirm RabbitMQ is actually listening on port 5672 inside the container:
- Run this command to enter the container shell:
docker exec -it rabbitmq bash - Check listening ports:
You should see a line showingnetstat -tulpn | grep 5672rabbitmq-serverlistening on0.0.0.0:5672. If not, restart the RabbitMQ service inside the container:rabbitmqctl stop && rabbitmq-server -detached
内容的提问来源于stack exchange,提问作者Jacek Andrzejczyk

