启动容器命令:
sudo docker run --name rabbit -d -p 5672:5672 -p 15672:15672 rabbitmq:management
producer代码:
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbit'))
channel = connection.channel()
channel.queue_declare(queue='task_queue', durable=True)
message = 'hello!'
channel.basic_publish(exchange='',
routing_key='task_queue',
body=message,
)
print(" [x] Sent %r" % message)
connection.close()
报错信息:
Traceback (most recent call last):
File "producer_queue.py", line 31, in <module>
main()
File "producer_queue.py", line 13, in main
connection = pika.BlockingConnection(pika.ConnectionParameters('rabbit'))
File "/usr/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 339, in __init__
self._process_io_for_connection_setup()
File "/usr/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 374, in _process_io_for_connection_setup
self._open_error_result.is_ready)
File "/usr/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 395, in _flush_output
raise exceptions.ConnectionClosed()
pika.exceptions.ConnectionClosed
我看同事和网上的代码 跟我的一样 但都可以直接在容器外使用服务
用另一种方式 挂载代码到一个python容器中 也可以正常使用
sudo docker run -v $PWD:/code -w /code --link=rabbit:rabbit -it python:2 bash
但是这样太麻烦了 也不合理
不知道哪里有问题 求解
世界只因有你2017-05-27 17:46:12
1.如果宿主机访问的话,你使用127.0.0.1 或者localhost试一试,先检查rabbitmq是否启动成功,然后检查端口服务,也检查一下日志
2.我推荐使用docker-compose编排服务,也推荐使用python挂载到容器中使用。