用python在docker上设置rabbitMQ

时间:2015-06-18 12:13:32

标签: python docker localhost rabbitmq microservices

我对docker相当新,我正在学习rabbitMQ。到目前为止,我已经能够在我的ubuntu vm上以python libary pika的形式运行rabbitMQ。这根本没有任何问题,但我现在把它放在docker中的一个小应用程序中,但是不起作用。

问题似乎出现在设置中,并且所有方式都失败了这行代码:

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

正在导入的变量:

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

文件:

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"

在此代码中,它总是在行上失败:

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

最后是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

有关所有文件的任何其他信息,请访问: https://github.com/CanopyCloud/microservice-python

1 个答案:

答案 0 :(得分:2)

您的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/
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

它没有/不正确的原因是因为您在CMD中定义了多个Dockerfile。我非常确定docker只会在结果图像中设置最后一个命令,而CMD不会"运行"作为图像构建过程的一部分; RUN确实如此。

CMD设置&#34;命令&#34;图像作为docker run <image>

的一部分运行

此外,您似乎已将RabbitMQ和您的Python应用程序组合到一个Docker镜像/容器中;这不是最好的事情。

应该而是将其分成两个图像。

  • RabbitMQ图像/容器
  • 您的App Image / Container

使用&#34; Docker Links&#34;通过docker run --link将容器链接在一起。

可以为您的Python APp构建一个Image,通过使用类似这样的内容作为Python App的单独Dockerfile

FROM python:2.7-onbuild

RUN pip install -r requirements.txt

ADD server.py /app

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