Jonathan
Jonathan

Reputation: 2653

Docker/Boot2Docker/Fig + RabbitMQ host issue; pika.exceptions.AMQPConnectionError: [Errno -2] Name or service not known

I have just started learning RabbitMQ and Docker/Fig. I have a very simple example with 3 fig containers, one each for the Rabbit Server, Producer, and Consumer. In /etc/hosts I have set my boot2docker ip to an alias of dockerhost:

/etc/hosts
192.168.59.103    dockerhost

When I fig up -d the RabbitMQ server, everything works as it should; I can open a browser and go to dockerhost:PORTNUM and access the RabbitMQ control panel.

In my RabbitMQ producer I have set the connection host to dockerhost:

/producer.py
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host=config['RabbitMQ']['hostname'],
    ...
    ))

where my config file looks like this:

/config.ini
[RabbitMQ]
hostname    = dockerhost

However when I run fig up -d on the RabbitMQ producer I receive a connection error:

rabbitworker_1 |   File "/usr/local/lib/python3.4/site-packages/pika/adapters/blocking_connection.py", line 301, in _adapter_connect
rabbitworker_1 |     raise exceptions.AMQPConnectionError(error)
rabbitworker_1 | pika.exceptions.AMQPConnectionError: [Errno -2] Name or service not known`

I can get the RabbitMQ producer to work properly by entering the boot2docker ip directly into the RabbitMQ hostname instead of the dockerhost alias:

/config.ini
[RabbitMQ]
hostname    = 192.168.59.103

The above change gets everything working properly.

My question is how can I get the RabbitMQ producer to work by using hostname = dockerhost instead of entering the actual IP address?

Update

Here is my RabbitMQ server fig.yml which works with dockerhost:

rabbit:
    image: mikaelhg/docker-rabbitmq
    ports:
     - "5672:5672"
     - "15672:15672"

And here is my RabbitMQ producer fig.yml and Dockerfile which does NOT work with dockerhost:

/fig.yml
rabbitworker:
    build: .
    volumes:
     - .:/opt/worker
     - ./log:/var/log
    command: supervisord -n

/Dockerfile
FROM python3-rabbit-producer

COPY . /opt/worker
WORKDIR /opt/worker

RUN pip3.4 install -r requirements.txt

CMD ["supervisord", "-n"]

Upvotes: 1

Views: 1894

Answers (1)

Céline Aussourd
Céline Aussourd

Reputation: 10664

As you are using fig, I suggest you use the real power of fig by creating 1 unique fig.yml file and link the containers for them to communicate. So you would edit your current RabbitMQ producer fig.yml so you get:

rabbit:
    image: mikaelhg/docker-rabbitmq
    ports:
     - "5672:5672"
     - "15672:15672”

rabbitworker:
    build: .
    volumes:
     - .:/opt/worker
     - ./log:/var/log
    links: 
       - rabbit
    command: supervisord -n

and then edit your config file to get:

/config.ini
[RabbitMQ]
hostname    = rabbit

If you really want to keep your config file as it is and use dockerhost, then you can edit the rabbitworker links in fig.yml this way:

    links: 
       - rabbit:dockerhost

but you have to be aware that dockerhost will resolve to your RabbitMQ server container IP (and not your host IP).

Upvotes: 1

Related Questions