0

I'm using docker to run two django services. RabbitMq is installed in the host not in a container. When I run the django development server without the container rabbitmq connection works. But when I use the docker-compose up it gives pika.exceptions.AMQPConnectionError error. I restarted the rabbitmq but it doesn't resolve the problem

Dockerfile

FROM python:3.9
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt
COPY . /app

CMD python manage.py runserver 0.0.0.0:8000

Dockercompose.yml

version: '3.3'
services:
  blog_writer:
    build:
      context: .
      dockerfile: DockerFile
    ports:
      - 8001:8000
    volumes:
      - .:/app

Producer.py

import json
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', heartbeat=600, blocked_connection_timeout=300))
channel = connection.channel()

def publish(method, body):
    properties = pika.BasicProperties(method)
    channel.basic_publish(exchange='', routing_key='blogs', body=json.dumps(body), properties=properties)

RabbitMq Status

sudo systemctl status rabbitmq-server.service 
[sudo] password for navanjane: 
● rabbitmq-server.service - RabbitMQ broker
     Loaded: loaded (/lib/systemd/system/rabbitmq-server.service; enabled; vend>
     Active: active (running) since Sat 2022-01-15 18:51:40 +0530; 1 day 5h ago
   Main PID: 1105 (beam.smp)
      Tasks: 28 (limit: 9342)
     Memory: 105.7M
     CGroup: /system.slice/rabbitmq-server.service
             ├─1105 /usr/lib/erlang/erts-12.1.5/bin/beam.smp -W w -MBas ageffcb>
             ├─1170 erl_child_setup 32768
             ├─1556 /usr/lib/erlang/erts-12.1.5/bin/epmd -daemon
             ├─1588 inet_gethost 4
             └─1589 inet_gethost 4

ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:   Doc guides:  https://r>
ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:   Support:     https://r>
ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:   Tutorials:   https://r>
ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:   Monitoring:  https://r>
ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:   Logs: /var/log/rabbitm>
ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:         /var/log/rabbitm>
ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:         <stdout>
ජන 15 18:51:35 navanjane-Ubuntu rabbitmq-server[1105]:   Config file(s): (none)
ජන 15 18:51:40 navanjane-Ubuntu rabbitmq-server[1105]:   Starting broker... com>
ජන 15 18:51:40 navanjane-Ubuntu systemd[1]: Started RabbitMQ broker.

  • `localhost` in Docker usually means "this container"; when your Python client code tries to connect to `localhost`, it's trying to connect to the Python client application, not RabbitMQ outside a container. Do any of the techniques in [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) help you here? – David Maze Jan 16 '22 at 20:32

0 Answers0