I want to test a docker image running a python script subscribing to a rabbitmq queue. I have rabbitmq running on my local machine, and want to test the docker container running on the same machine and have it subscribe to the local rabbimq server.
I want the script to read environment variables 'QUEUE_IP' set in the docker run command.
The python script:
#!/usr/bin/env python
import pika
host = os.environ.get('QUEUE_IP')
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=host))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
It doesnt work if QUEUE_IP = 127.0.0.1, and I also tried using the local ip address of the machine, but I only get
pika.exceptions.ProbableAuthenticationError
Is there any easy way of accessing the local rabbitmq from the docker container ?
A solution that works is to simply add the --net=host parameter to docker run, e.g.:
docker run -d --net=host my/container
This way, the host's network is shared with the container and it can access the rabbimq server with the localhost ip (127.0.0.1)