用python在docker上设置rabbitMQ

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

这个问题似乎是在设置和所有的方式失败了这一行的代码:

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

正在导入的variables:

 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

你的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中定义了多个CMD 。 我非常确定,docker只会在结果图像中设置最后一个命令,而CMD不会“运行”作为图像构build过程的一部分; RUN

CMD设置图像作为docker run <image>一部分运行的“命令”


此外,您似乎已经将RabbitMQ和您的Python应用程序组合到一个Docker Image / Container中; 这不是真正的最好的事情在这里做。

应该把它分成两个图像。

  • 一个RabbitMQ图像/容器
  • 您的应用程序图像/容器

并通过docker run --link使用“docker链接” – 链接将容器链接在一起。


可以很容易地为你的Python APP构build一个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"]