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.

  • Health Check command for docker(1.12) container (Not in Dockerfile!)
  • Docker Swarm - get services by secret
  • containerized nginx log rotation with logrotate
  • Protractor in a Docker container - Wait for full page to load
  • How to set the command history in a Dockerfile
  • How can I run multiple docker nodes on my laptop to simulate a cluster?
  • 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 upgrade npm a inside docker container?
  • SpringBoot with Tomcat uses around 190mb of memory. What processes are running within Springboot?
  • Cannot mount data volume in Docker with Windows
  • PostgreSQL on AWS ECS: psycopg2.OperationalError invalid port number 5432
  • Getting “FATAL: role ”root“ does not exist” when trying to do “docker up”
  • How to set a var for a Docker-compose file?
  • 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.