用python在docker上设置rabbitMQ

2024-05-23 16:46:52 发布

您现在位置:Python中文网/ 问答频道 /正文

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

问题似乎出在设置中,但它始终未能通过这行代码:

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

导入的变量:

^{pr2}$

文件:

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


Tags: runhostmessageportserviceexceptionconnectionmicro
1条回答
网友
1楼 · 发布于 2024-05-23 16:46:52

您的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不会作为图像构建过程的一部分“运行”东西;RUN会这样做。在

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


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

你应该把它分成两个图像。在

  • RabbitMQ图像/容器
  • 你的应用程序映像/容器

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


通过使用类似于这样的东西作为Python应用程序的一个单独的Dockerfile,您可以非常容易地为Python应用程序构建一个映像:

^{pr2}$

相关问题 更多 >