RabbitMQ connection refused
Similar to what is told on documentation I created an container with:
$ docker run -d -p 5672:5672 -h myrabbit --name new-rabbit rabbitmq
If I do docker exec into the container I can execute successfully rabbitmqctl subcommands, so I know that this containter and RabbitMQ instance is working.
But from the host I can't connect to the RabbitMQ server.
I have tried with telnet
$ telnet 127.0.0.1 5672
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
I have done one of RabbitMQ tutorials: Tutorial 1 - Hello World With this change:
# ...
cred = pika.PlainCredentials('guest', 'guest')
params = pika.ConnectionParameters(
host='127.0.0.1',
port=5672,
virtual_host='/',
credentials=cred
)
connection = pika.BlockingConnection(params)
channel = connection.channel()
#...
This not work either.
I do even tried with Celery example. Did not connect successfully.
What could be missing to successfully connect into this RabbitMQ server?
Are you on OSX or Windows? If you are connecting from your host, you'll need to hit the virtual machine's ip address. You can get it with docker-machine ip default.
@yosifkit OSX! :smiley: I suppose that RabbitMQ requires that IP and ignores 127.0.0.1 that I have used.
Because on Nginx and other images I recall that I used 127.0.0.1 and worked.
I will try it. Thanks! :+1:
@yosifkit You were right! After copy-pasted from docker-machine ip default the code worked.
Was a distraction of mine not to consider the IP of the machine instead of localhost but like I said with other images/containers, localhost works fine.
Again, thanks very much. :+1:
use: docker inspect <container-id> to check the ip.
I am running RabbitMQ using this docker run -d --restart=always -h localrabbit --name rabbitmq -p 5672:5672 rabbitmq:3-management
I tried both localhost and container IP (using docker inspect
got the solution from http://stackoverflow.com/questions/25869858/celery-error-in-connecting-to-rabbitmq-server sudo rabbitmqctl add_user jm-user1 sample
Then I set the permissions again with this:
sudo rabbitmqctl set_permissions -p jm-vhost jm-user1 "." "." ".*"
Restarted rabbitmq server to make sure the changes take effect and made modifications to tasks.py:
app = Celery('tasks', broker='amqp://jm-user1:sample@localhost/jm-vhost')
In my case the port was closed: nmap -p 5672 localhost gave: 5672/tcp closed amqp. So I stopped and restarted the container with port forwarding
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq rabbitmq
and now nmap says 5672 is open
What @albertocap said was the solution my case also. I had no issues on my local development machine but once I ran the container on a DO droplet I had to explicitly forward the 5672 port.
In my case the port was closed: nmap -p 5672 localhost gave: 5672/tcp closed amqp. So I stopped and restarted the container with port forwarding
docker run -d -p 5672:5672 -p 15672:15672 --name rabbitmq rabbitmqand now nmap says 5672 is open
this worked for me 👍 !! thanks
@tinynet Thank you! I used -p 5672:5672 but didn't realize I would need to do -p 15672:15672 as well.
I met this issue today, and found it caused by RabbitMQ.Client version(5.2.0), then I updated the version from 5.2.0 to 5.1.1, after that, run command docker-compose up --build, it works.
# docker-compose.yml
version: '3.4'
services:
publisher_api:
build: ./publisher_api
restart: always
subscriber_console:
build: ./subscriber_console
restart: always
depends_on:
- "publisher_api"
- "rabbitmq"
rabbitmq: # login guest:guest
image: rabbitmq:3-management
hostname: "rabbitmq"
labels:
NAME: "rabbitmq"
ports:
- "5672:5672"
- "15672:15672"
| image | tag |
|---|---|
| rabbitmq | 3-management |
# my app.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="newtonsoft.json" Version="12.0.3" />
<PackageReference Include="rabbitmq.client" Version="5.1.1" />
</ItemGroup>
</Project>