gpt4 book ai didi

python - 使用 python 在 docker 上设置rabbitMQ

转载 作者:行者123 更新时间:2023-12-01 04:40:07 25 4
gpt4 key购买 nike

我对 docker 相当陌生,我正在学习rabbitMQ。到目前为止,我已经能够在我的 ubuntu 虚拟机上以 python 库 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

最佳答案

您的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 (s) 在您的 Dockerfile 中。我很确定 docker 只会设置结果图像中的最后一个命令和 CMD不将“运行”作为镜像构建过程的一部分; RUN确实如此。

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

<小时/>

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

应该将其分成两个图像。

  • RabbitMQ 镜像/容器
  • 您的应用镜像/容器

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

<小时/>

可以通过使用类似的东西作为单独的Dockerfile来非常轻松地为您的Python应用程序构建图像。对于您的 Python 应用程序:

FROM python:2.7-onbuild

RUN pip install -r requirements.txt

ADD server.py /app

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

关于python - 使用 python 在 docker 上设置rabbitMQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com