2015-06-18 28 views
1

我對docker相當陌生,我正在學習rabbitMQ。到目前爲止,我已經能夠在我的ubuntu vm上以python libary pika的形式運行rabbitMQ。這工作沒有問題,但我現在把它放到docker中的一個小應用程序,並不起作用。在python上設置docker上的rabbitMQ

這個問題似乎是在建立和它所有的方法失敗,這行代碼:被導入

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

回答

0

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 

它不是/不正確的原因是因爲你在你的Dockerfile defning多個CMD(S)。我非常確定碼頭工人只會在生成的圖像中設置最後的命令,並且CMD不會「運行」作爲圖像生成過程的一部分; RUN呢。

CMD樹立「命令」,該圖像爲docker run <image>


也有一部分你似乎已經合併的RabbitMQ和您的Python應用程序到一個泊塢圖片/容器中運行;這其實並不是最好的這裏要做的事情。

應該而不是將其分成兩個圖像。

  • 一個RabbitMQ的圖片/集裝箱
  • 應用圖片/集裝箱

並使用 「泊塢窗鏈接」 通過docker run --link到容器連接在一起。


可以通過使用這樣的事情作爲您的Python應用程序單獨Dockerfile很容易地建立您的Python應用程序的圖片:

FROM python:2.7-onbuild 

RUN pip install -r requirements.txt 

ADD server.py /app 

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

由於這是真正有用的,我會讓你知道它是否工作後,我實現它:) –