我對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
由於這是真正有用的,我會讓你知道它是否工作後,我實現它:) –