pika.exceptions.AMQPConnectionError - Error while running docker compose

I have a python docker container that is dependent on rabbitmq. I tried to link these two containers using docker-compose file:

version: "3.8"
services:
  rabbitmq:
    image: rabbitmq:3-management
    hostname: my-rabbit
    volumes:
      - ./rabbitmq/etc/configurations.json:/etc/rabbitmq/configurations.json
      - ./rabbitmq/etc/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf
    ports:
      - 5672:5672
      - 15672:15672
      - 25672:25672
    networks:
      - ihddocker

  notification:
    image: ihdnotification
    restart: on-failure
    links:
      - rabbitmq
    depends_on:
      - rabbitmq
    networks:
      - ihddocker

networks: 
  ihddocker:
    external:
      name: ihddocker

I am getting the following error in python app container -

File "broker.py", line 22, in start_consumer
connection = pika.BlockingConnection(message_broker_params)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 359, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "/usr/local/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 450, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError

This is the AMQP_SERVER_URL=amqp://guest:guest@localhost:5672
I am not sure what caused the problem.

This only guarantees that the container for RabbitMQ will be started before the dependent container is started. But it does not guarantee that RabbitMQ will be fully up and running. Could it be that your Python app is trying too soon? If you did not do that already, you could try without Compose, to explicitly start the Python container a bit later and see if things work then. Or start the Python container too early and see if that gives you the same error.

(And if that helps, then you may want to make your Python app resilient for this kind of problems. Of course, you could wait until RabbitMQ has finished starting up, like explained in the documentation, but you may also want the application to recover from temporary later problems anyway?)

I tried running the RabbitMQ container first without docker compose using the following command:
docker run --rm -it -p 15672:15672 -p 5672:5672 rabbitmq:3-management
before trying to start the python container but it throws the same error and vice-versa.

This works for me; http://localhost:15672 is available for a browser on the host, and authenticating using guest/guest works too. So, the published ports allow any software running on the host to access it. However, you did not specify a shared network on the docker run command line, so how would another container access it? And, aha, I guess this is your problem in Compose too, as localhost in the following won’t work:

For the Compose version from your first post, you’ll need amqp://guest:guest@rabbitmq:5672 or amqp://guest:guest@my-rabbit:5672. See also Networking in Compose | Docker Docs.

http://localhost:15672 is available and I’m also able to authenticate using guest / guest but the problem is connecting this to python container.

I tried replacing locahost to amqp://guest:guest@my-rabbit:5672 and amqp://guest:guest@rabbitmq:5672, it throws the following error in the python container :

2021-07-13 08:03:12 INFO     notification :: Starting the consumer...
2021-07-13 08:03:18 ERROR    pika.adapters.utils.selector_ioloop_adapter :: Address resolution failed: gaierror(-2, 'Name or service not known')
2021-07-13 08:03:18 ERROR    pika.adapters.utils.connection_workflow :: getaddrinfo failed: gaierror(-2, 'Name or service not known').
2021-07-13 08:03:18 ERROR    pika.adapters.utils.connection_workflow :: AMQP connection workflow failed: AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - gaierror(-2, 'Name or service not known'); first exception - None.
2021-07-13 08:03:18 ERROR    pika.adapters.utils.connection_workflow :: AMQPConnectionWorkflow - reporting failure: AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - gaierror(-2, 'Name or service not known'); first exception - None
2021-07-13 08:03:18 ERROR    pika.adapters.blocking_connection :: Connection workflow failed: AMQPConnectionWorkflowFailed: 1 exceptions in all; last exception - gaierror(-2, 'Name or service not known'); first exception - None
2021-07-13 08:03:18 ERROR    pika.adapters.blocking_connection :: Error in _create_connection().

tried running docker in a network using the following command

docker run -d --network ihdnetwork --name rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq:3-management

and python container using

docker run -d --network ihdnetwork -p 8085:8085 ihdnotification

it still returns the

pika.exceptions.AMQPConnectionError

Thanks for the help, I’m new to Docker and still figuring out lots of things.

I’d expect that the above should work with amqp://guest:guest@rabbitmq:5672 (but not with my-rabbit, which you did not mention above). You don’t even need to publish any ports for that.

Did you try with Compose again? Can you somehow validate that your “ihdnotification” works without Docker (like running that “ihdnotification” locally without Docker, making it refer to localhost along with docker run --rm -p 15672:15672 -p 5672:5672 rabbitmq:3-management)?

The following allows me to connect to RabbitMQ from busybox just fine, on a Mac:

# Use the default network that Docker creates for the first container:
docker run --rm --name rabbitmq rabbitmq:3-management
docker run --rm --network container:rabbitmq -it busybox

Same goes for:

docker network create --driver bridge my-network

# Use an explicit network for both containers:
docker run --rm --network my-network --name rabbitmq rabbitmq:3-management
docker run --rm --network my-network -it busybox

In both cases, the following works on the busybox prompt:

/ # wget rabbitmq:15672
Connecting to rabbitmq:15672 (172.20.0.2:15672)
saving to 'index.html'
index.html           100% |********************************|  2884  0:00:00 ETA
'index.html' saved

/ # wget -O- http://guest:guest@rabbitmq:15672/api/vhosts
Connecting to rabbitmq:15672 (172.20.0.2:15672)
writing to stdout
[{"cluster_state":{"rabbit@c7f63484c78f":"running"},"description":"Default virtual host","metadata":{"description":"Default virtual host","tags":[]},"name":"/",-                    100% |********************************|   187  0:00:00 ETA
written to stdout

/ # exit
1 Like

Using docker, today, I had the same problem and solve it using the service name not localhost!

I created my account for replying to your message!
You save my day dude!
Your service name solution works for me, thankful for that!
@eric6841