How to connect to the rabbitMQ docker container?

Groot221 :

I am spawning a rabbitMQ container with the command -

docker run -d --hostname localhost --name rabbit-tox rabbitmq:3

and this is the docker ps -a output -

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

docker inspect 6d95830a43d9 output --

[
    {
        "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,

I am trying to connect to the container using the code -

#!/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()

but it gives out the error -

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

Sorry, I am new to rabbitMQ, any help would be appreciated.

Thanks.

DuDoff :

There are two problems I can see directly...

  1. No username/password
  2. No port forwarding.

This is 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"]

This is 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 $@

Put the Dockerfile and the init.sh in one folder and then do: docker build -t 'my_rabbit' . - This will build your image.

Then do docker run -p5672:5672 -p15672:15672 my_rabbit

5672 - This is the port RabbitMQ sends it's messages on.

15672 - This is the port RabbitMQ's management GUI.

If you are running this on your localmachine, you can navigate to: localhost:15672 and enter username: user and password: password and voila. It should all work just fine!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=220131&siteId=1