Setting up rabbitMQ on docker with python

4.9k views Asked by At

I am fairly new to docker and I am learning about rabbitMQ. So far I have been able to run rabbitMQ, in the form of the python libary pika, on my ubuntu vm. This worked with no problems at all but I have now put it onto a small app in docker and does not work.

The problem seems to be in the set up and it all ways fails this line of code:

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host=HOST, port=80, credentials=credentials))

The Variables being imported:

USER = "test"
PASS = "testpass1"
HOST = "dockerhost"

The file:

import pika
from settings import USER, PASS, HOST

def send(message):

    message = str(message)
    print 'trying: credentials = pika.PlainCredentials(username=USER, password=PASS)'
    try:
        credentials = pika.PlainCredentials(username=USER, password=PASS)
    except Exception:
        print 'Failed'
        print str(Exception)
        return 'Failed on: credentials = pika.PlainCredentials(username=USER, password=PASS) \n' + str(Exception.message)

    print 'trying: connection = pika.BlockingConnection(pika.ConnectionParameters(host=HOST, port=80, credentials=credentials))'
    try:
        connection = pika.BlockingConnection(pika.ConnectionParameters(
            host=HOST, port=80, credentials=credentials))
    except Exception:
        print 'Failed'
        print str(Exception)
        return 'Failed on: connection = pika.BlockingConnection(pika.ConnectionParameters(host=HOST, port=80, credentials=credentials)) \n' + str(Exception.message)

    channel = connection.channel()

    channel.queue_declare(queue='hello')

    channel.basic_publish(exchange='',
                      routing_key='hello',
                      body=message)
    connection.close()

    return "Message Sent"

Within this code it always fails on the line:

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host=HOST, port=80, credentials=credentials))

And finally the Dockerfile:

FROM ubuntu
MAINTAINER Will Mayger
RUN echo "deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -sc) main universe" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
RUN apt-get install -y python python-dev python-distribute python-pip
RUN git clone https://github.com/CanopyCloud/microservice-python
RUN pip install -r /microservice-python/requirements.txt
EXPOSE 80
WORKDIR /microservice-python/
CMD sudo rabbitmqctl add_user test testpass1
CMD sudo rabbitmqctl add_vhost myvhost
CMD sudo rabbitmqctl set_permissions -p myvhost test ".*" ".*" ".*"
CMD sudo rabbitmq-server

CMD python /microservice-python/server.py

For any additional information all the files are located on: https://github.com/CanopyCloud/microservice-python

1

There are 1 answers

0
James Mills On

Your Dockerfile is not correct.

Try this instead:

FROM ubuntu
MAINTAINER Will Mayger
RUN echo "deb http://archive.ubuntu.com/ubuntu/ $(lsb_release -sc) main universe" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
RUN apt-get install -y python python-dev python-distribute python-pip
RUN git clone https://github.com/CanopyCloud/microservice-python
RUN pip install -r /microservice-python/requirements.txt
EXPOSE 80
WORKDIR /microservice-python/
RUN sudo rabbitmqctl add_user test testpass1
RUN sudo rabbitmqctl add_vhost myvhost
RUN sudo rabbitmqctl set_permissions -p myvhost test ".*" ".*" ".*"
RUN sudo rabbitmq-server

CMD python /microservice-python/server.py

The reason it isn't/wasn't correct was because you were defning multiple CMD(s) in your Dockerfile. I'm pretty sure docker will only set the last command in the resulting image and CMD does not "run" things as part of the image build process; RUN does.

CMD sets up the "command" that the image runs as part of docker run <image>


Also you seem to have combined RabbitMQ and your Python app into the one Docker Image/Container; which isn't really the best thing to do here.

You should instead split this out into two images.

  • A RabbitMQ Image/Container
  • Your App Image/Container

And use "Docker Links" via docker run --link to link the containers together.


You can build an Image for your Python APp quite easily by using something like this as a separate Dockerfile for your Python App:

FROM python:2.7-onbuild

RUN pip install -r requirements.txt

ADD server.py /app

WORKDIR /app
CMD ["python", "./server.py"]