简体   繁体   English

如何连接rabbitMQ docker容器?

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

I am spawning a rabbitMQ container with the command -我正在使用命令生成一个 rabbitMQ 容器 -

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

and this is the docker ps -a output -这是 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

docker inspect 6d95830a43d9 output --码头工人检查 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,

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.对不起,我是rabbitMQ 的新手,任何帮助将不胜感激。

Thanks.谢谢。

There are two problems I can see directly...有两个问题我可以直接看到......

  1. No username/password没有用户名/密码
  2. No port forwarding.没有端口转发。

This is Dockerfile这是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这是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' .Dockerfileinit.sh放在一个文件夹中,然后执行: docker build -t 'my_rabbit' . - This will build your image. - 这将建立你的形象。

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

5672 - This is the port RabbitMQ sends it's messages on. 5672 - 这是 RabbitMQ 发送消息的端口。

15672 - This is the port RabbitMQ's management GUI. 15672 - 这是端口 RabbitMQ 的管理 GUI。

If you are running this on your localmachine, you can navigate to: localhost:15672 and enter username: user and password: password and voila.如果您在localhost:15672上运行它,您可以导航到: localhost:15672并输入用户名: user和密码: password和瞧。 It should all work just fine!它应该一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM