Rabbitmq image using docker cannot be exposed (providing services to the outside world)

  docker, question

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 think my colleagues and online code are the same as mine, but they can use the service directly outside the container.

Loading code into a python container in another way can also be used normally.

sudo docker run -v $PWD:/code -w /code --link=rabbit:rabbit -it python:2 bash

But this is too much trouble and unreasonable

I don’t know where there is a problem to solve.

1. If the host accesses, you can use 127.0.0.1 or localhost to give it a try, first check whether rabbitmq started successfully, then check the port service and also check the log

2. I recommend using docker-compose orchestration service and python mount to container.