簡體   English   中英

如何連接rabbitMQ docker容器?

[英]How to connect to the rabbitMQ docker container?

我正在使用命令生成一個 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 的新手,任何幫助將不勝感激。

謝謝。

有兩個問題我可以直接看到......

  1. 沒有用戶名/密碼
  2. 沒有端口轉發。

這是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 $@

Dockerfileinit.sh放在一個文件夾中,然后執行: docker build -t 'my_rabbit' . - 這將建立你的形象。

然后做docker run -p5672:5672 -p15672:15672 my_rabbit

5672 - 這是 RabbitMQ 發送消息的端口。

15672 - 這是端口 RabbitMQ 的管理 GUI。

如果您在localhost:15672上運行它,您可以導航到: localhost:15672並輸入用戶名: user和密碼: password和瞧。 它應該一切正常!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM