user2130951
user2130951

Reputation: 2741

Docker, rabbitMQ and pike connection refused

I am trying to connect to rabbitMQ from inside docker, using docker-compose. There are no problems connecting to the "image: rabbitmq:3-management" from a program running outside the container.

There are no passwords set or anything.

After looking a bit more at the error message it seems like pika tries to connect with IPv6 instead of IPv4. Looking at the documentation I can't figure out a way to connect with IPv4.

Failure happens on

self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))

Error log
rabbitMQ_1  | 2020-12-09 12:41:42.332 [info] <0.685.0> Ready to start client connection listeners
rabbitMQ_1  | 2020-12-09 12:41:42.333 [info] <0.988.0> started TCP listener on [::]:5672
listener_1  | ERROR:pika.adapters.utils.io_services_utils:Socket failed to connect: <socket.socket fd=9, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 36782)>; error=111 (Connection refused)
listener_1  | ERROR:pika.adapters.utils.connection_workflow:TCP Connection attempt failed: ConnectionRefusedError(111, 'Connection refused'); dest=(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('127.0.0.1', 5672))
listener_1  | ERROR:pika.adapters.utils.connection_workflow:AMQPConnector - reporting failure: AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused')
listener_1  | ERROR:pika.adapters.utils.io_services_utils:<socket.socket fd=9, family=AddressFamily.AF_INET6, type=SocketKind.SOCK_STREAM, proto=6, laddr=('::', 0, 0, 0)>.connect(('::1', 5672, 0, 0)) failed: OSError(99, 'Cannot assign requested address')
listener_1  | ERROR:pika.adapters.utils.connection_workflow:TCP Connection attempt failed: OSError(99, 'Cannot assign requested address'); dest=(<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('::1', 5672, 0, 0))
listener_1  | ERROR:pika.adapters.utils.connection_workflow:AMQPConnector - reporting failure: AMQPConnectorSocketConnectError: OSError(99, 'Cannot assign requested address')
listener_1  | ERROR:pika.adapters.utils.connection_workflow:AMQP connection workflow failed: AMQPConnectionWorkflowFailed: 2 exceptions in all; last exception - AMQPConnectorSocketConnectError: OSError(99, 'Cannot assign requested address'); first exception - AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused').
listener_1  | ERROR:pika.adapters.utils.connection_workflow:AMQPConnectionWorkflow - reporting failure: AMQPConnectionWorkflowFailed: 2 exceptions in all; last exception - AMQPConnectorSocketConnectError: OSError(99, 'Cannot assign requested address'); first exception - AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused')
listener_1  | ERROR:pika.adapters.blocking_connection:Connection workflow failed: AMQPConnectionWorkflowFailed: 2 exceptions in all; last exception - AMQPConnectorSocketConnectError: OSError(99, 'Cannot assign requested address'); first exception - AMQPConnectorSocketConnectError: ConnectionRefusedError(111, 'Connection refused')
listener_1  | ERROR:pika.adapters.blocking_connection:Error in _create_connection().
listener_1  | Traceback (most recent call last):
listener_1  |   File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 450, in _create_connection
listener_1  |     raise self._reap_last_connection_workflow_error(error)
listener_1  | pika.exceptions.AMQPConnectionError

docker-compose.yml

version: '3'
services: 
  rabbitMQ:
    image: rabbitmq:3-management
    ports:
      - "15672:15672"
      - "5672:5672"
  login:
    build:
      context: .
      dockerfile: Dockerfile.login
    restart: on-failure
  listener:
    build:
      context: .
      dockerfile: Dockerfile.listener
    restart: on-failure

Upvotes: 6

Views: 5867

Answers (1)

user2130951
user2130951

Reputation: 2741

The docker-compose does not setup a localhost network. This means that each service has their own name.

In this case the host should be changed from:

self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))

to:

self.connection = pika.BlockingConnection(pika.ConnectionParameters(host='rabbitMQ'))

Upvotes: 7

Related Questions