Start container command:
sudo docker run --name rabbit -d -p 5672:5672 -p 15672:15672 rabbitmq:management
producer code:
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()
Error message:
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
I saw the codes from colleagues and online are the same as mine, but they can use services directly outside the container
Another way to mount the code into a python container can also be used normally
sudo docker run -v $PWD:/code -w /code --link=rabbit:rabbit -it python:2 bash
But this is too troublesome and unreasonable
I don’t know where the problem is. Please solve it
世界只因有你2017-05-27 17:46:12
1. If the host is accessed, try using 127.0.0.1 or localhost. First check whether rabbitmq is started successfully, then check the port service, and also check the log
2. I recommend using docker-compose orchestration service, and I also recommend using python to mount it into a container.