Accessing rabbitmq running on local machine from docker container
Question: 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_URL' set in the docker
run command.

The python script:

    
    
    #!/usr/bin/env python
    import pika
    
    url = os.environ.get('QUEUE_URL')
    
    params.socket_timeout = 5
    connection = pika.BlockingConnection(pika.ConnectionParameters(
        host=url))
    
    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()
    

Obviously it doesnt work if QUEUE_URL = localhost, 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 ?


Answer: According to [Docker CLI
docs](https://docs.docker.com/engine/reference/commandline/run/):

> Sometimes you need to connect to the Docker host from within your container.
> To enable this, pass the Docker host’s IP address to the container using the
> --add-host flag. To find the host’s address, use the `ip addr show` command.

So all you need to do is set: `QUEUE_URL` to the output of `ip addr show`.

