docs icon indicating copy to clipboard operation
docs copied to clipboard

RabbitMQ connection refused

Open poliveira89 opened this issue 10 years ago • 11 comments

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?

poliveira89 avatar Jan 26 '16 00:01 poliveira89

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 avatar Jan 26 '16 17:01 yosifkit

@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:

poliveira89 avatar Jan 26 '16 18:01 poliveira89

@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:

poliveira89 avatar Jan 27 '16 15:01 poliveira89

use: docker inspect <container-id> to check the ip.

mlyczba avatar Dec 20 '16 10:12 mlyczba

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 ) still no luck. Still getting "cannot connect to amqp://guest:**@localhost:5672//: timed out". Any help would be greatly appreciated.

arulrajalivi avatar May 16 '17 23:05 arulrajalivi

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')

arulrajalivi avatar May 16 '17 23:05 arulrajalivi

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

tinynet avatar Jun 13 '17 17:06 tinynet

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.

raduciobanu22 avatar Jul 16 '17 12:07 raduciobanu22

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

this worked for me 👍 !! thanks

chiajr avatar Jan 08 '19 22:01 chiajr

@tinynet Thank you! I used -p 5672:5672 but didn't realize I would need to do -p 15672:15672 as well.

ben-dyer avatar Aug 27 '19 14:08 ben-dyer

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>

simonmatt avatar May 23 '20 12:05 simonmatt