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.

  • Running atop in a docker stack
  • How can JVMs running inside Bluemix container groups be monitored?
  • Start DB2 server in docker container on a specific date without changing host date
  • using docker for quick switching iojs version
  • Unable to start jetty container on Docker cloud
  • Connect to Remote MSSQL db from Linux Docker container
  • 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"]
    

  • how to set docker container hostname/IP permanently
  • proxy_pass not finding container using dockergen
  • Docker Rest Apis using node.js
  • How do I change the umask of a shared docker volume?
  • Eclipse che cannot deploy web application archive
  • How to merge host folder with container folder in Docker?
  • One Solution collect form web for “Docker/Boot2Docker/Fig + RabbitMQ host issue; pika.exceptions.AMQPConnectionError: [Errno -2] Name or service not known”

    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).

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.