python - 如何连接到rabbitMQ docker容器?
问题描述
我正在使用命令生成一个 rabbitMQ 容器 -
docker run -d --hostname localhost --name rabbit-tox rabbitmq:3
这是 docker ps -a 输出 -
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6d95830a43d9 rabbitmq:3 "docker-entrypoint..." 6 minutes ago Up 6 minutes 4369/tcp, 5671-5672/tcp, 25672/tcp rabbit-tox
码头工人检查 6d95830a43d9 输出——
[
{
"Id": "6d95830a43d90557009a783779442927ca4bf211198f5c4eb420b7bb78b5de08",
"Created": "2020-03-12T15:34:12.661119753Z",
"Path": "docker-entrypoint.sh",
"Args": [
"rabbitmq-server"
],
"State": {
"Status": "running",
"Running": true,
. . .
"EndpointID": "",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "",
"EndpointID": "",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
我正在尝试使用代码连接到容器 -
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
但它给出了错误 -
Traceback (most recent call last):
File "rmqtest.py", line 4, in <module>
connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1'))
File "/home/mlokur/venv/lib/python3.7/site-packages/pika/adapters/blocking_connection.py", line 359, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "/home/mlokur/venv/lib/python3.7/site-packages/pika/adapters/blocking_connection.py", line 450, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError
对不起,我是rabbitMQ的新手,任何帮助将不胜感激。
谢谢。
解决方案
有两个问题我可以直接看到...
- 没有用户名/密码
- 没有端口转发。
这是Dockerfile
FROM rabbitmq:management
# Define environment variables.
ENV RABBITMQ_DEFAULT_USER user
ENV RABBITMQ_DEFAULT_PASS password
ADD init.sh /init.sh
RUN ["chmod", "+x", "/init.sh"]
EXPOSE 15672
# Define default command
CMD ["/init.sh"]
这是init.sh
#!/bin/sh
# Create Rabbitmq user
( sleep 10 ; \
rabbitmqctl add_user user password ; \
rabbitmqctl set_user_tags user administrator ; \
rabbitmqctl set_permissions -p / user ".*" ".*" ".*" ; \
echo "*** User 'user' with password 'password' completed. ***" ; \
echo "*** Log in the WebUI at port 15672 (example: http:/localhost:15672) ***") &
# $@ is used to pass arguments to the rabbitmq-server command.
# For example if you use it like this: docker run -d rabbitmq arg1 arg2,
# it will be as you run in the container rabbitmq-server arg1 arg2
rabbitmq-server $@
将Dockerfile
和init.sh
放在一个文件夹中,然后执行以下操作:docker build -t 'my_rabbit' .
- 这将构建您的图像。
然后做docker run -p5672:5672 -p15672:15672 my_rabbit
5672
- 这是 RabbitMQ 发送消息的端口。
15672
- 这是端口 RabbitMQ 的管理 GUI。
如果您在本地机器上运行它,您可以导航到:localhost:15672
并输入用户名:user
和密码:password
然后瞧。它应该一切正常!
推荐阅读
- c++ - CEPH + Librados++ 没有返回一致的结果
- javascript - React 中的状态未定义
- ms-access - 根据传递某个日期的另一个日期字段更新文本字段的值
- prolog - Prolog欧几里得广场
- c# - 如何在 C# 中拆分多节点 XML 文件
- mysql - 子查询返回超过 1 个值。当子查询跟随或将子查询用作表达式时,这是不允许的
- java - 从 JPA 查询返回页面对象
- boost - ARM 的交叉编译 boost 1.69.0
- scala - 当 IO 卡在中间时如何编写 doobie 事务
- apache - 主域的 Apache 虚拟主机返回 400 错误请求